Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between ` ` and ' '? [duplicate]

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);
          });
like image 476
Purushotham Kumar Avatar asked Feb 13 '26 08:02

Purushotham Kumar


1 Answers

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);
like image 169
Suren Srapyan Avatar answered Feb 14 '26 22:02

Suren Srapyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!