Hey guys i want to know that what is the difference between these two type of single quotes` and '
i tried to execute the below command with ` then it worked
axios.get(`https://api.github.com/users/${this.state.userName}`)
.then(resp => {
console.log(resp);
});
but when i tried to run the same command using '' it doesnt work
axios.get('https://api.github.com/users/${this.state.userName}')
.then(resp => {
console.log(resp);
});
The first is called template literals added in the ES6 - It first evaluates the given embedded expressions, gets the results and replaces the expressions with the results. In the ${} you give expressions (variable, function call, ...), and it creates a string replacing the ${expression} part with the result.
This code part
var str = 'text';
var template = `Some ${str}`; // `str` is a expression which returns the value
console.log(template);
is equivalent of concatenation of strings.
var str = 'text';
var template = 'Some ' + str; // Just concatenate the strings
console.log(template);
Template literals also allow you to create multi-line strings
const multiLine = `This is a multi-
line text`;
console.log(multiLine);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With