Template literal: It is used to combine variables and strings.
Old:
var no = 109
var name = " shivaji "
console.log("number is : "+no + " name is : " + name)
New syntax:
var no = 109
var name = " shivaji "
console.log(`number is : ${no}name is : ${ name}`)
Multi-line string:
Old:
var name = "shivaji \\n kondeti"
console.log(name}
New syntax: with the help of the backtick symbol ( ` )
var name = `shivaji
kondeti`
console.log(name}
Note: Based on your left space, it will display.
- If we invoke a function using template strings, we can actually safely accept arguments and pass them into a parameterised function.
- First, we get an array of all the string pieces.
- After the array of all the string pieces, the subsequent arguments will be the values that are being interpolated.
- If you don't know exactly how many values someone is passing, there's no way for you to really scale it. You could say Arg1, Arg2, go through all the way to Arg100, just in case there are a hundred arguments, but that's no way to live your life, right?
- For this, we can use the ES6 rest operator, which is three dots. If we do ... values, what it's going to do is take the rest of the values that are passed to that argument, and put them into an array for us.
n_string+1
n_interpolated strings