Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a var inside a string

I'm new to JS and I want to know how to print a var inside a string , i found that the way should be :

var user = {
    name: "Mike",
    sayHi:() => {
        console.log('Hi, I\'m ${name}');
    }
};

user.sayHi()

but I get : Hi, I'm ${name}

like image 563
H.Epstein Avatar asked Mar 21 '18 15:03

H.Epstein


People also ask

Can you put a variable in a string?

You can specify your variables directly in the strings. $message = "Hello, $first $last." The type of quotes you use around the string makes a difference. A double quoted string allows the substitution but a single quoted string doesn't.

How do you print a variable in a method?

To print the variable in Java, there are three print methods of the System class: print(), println(), and printf() methods. The print() method prints out all statements in a single line. Whereas the println() method prints the value of the added variables or text in separate lines.


1 Answers

Template literals use backticks `` instead of regular quotes ''.

like image 140
Ryu Miu Avatar answered Oct 01 '22 06:10

Ryu Miu