
Write cleaner code with Template Literals in JavaScript
We use template literals to write clean and readable code because traditional string concatenation has problem of readability. Code Example : Without template literals const name = " John " ; const age = 20 ; const message = " My name is " + name + " and I am " + age + " years old. " ; console . log ( message ); To many "+" sign Difficult to understand. With Template literals const name = " John " ; const age = 20 ; const message = `My name is ${ name } and I am ${ age } years old.` ; console . log ( message ); How to write Template literals (syntax) : We use backticks \ to write template literal. Inside the backticks you can embed(insert) your variables and valid JavaScript expression. // use of variables inside the string. const message = `My name is ${ name } and I am ${ age } years old.` ; //valid JavaScript expression inside the string. console . log ( `Sum is ${ a + b } ` ); So what are template literals ? In JavaScript, template literals (also known as template strings) . It all
Continue reading on Dev.to Beginners
Opens in a new tab




