Last active
November 12, 2018 07:37
-
-
Save emfluenceindia/a1b6271bc377746ea78eb395163ed66e to your computer and use it in GitHub Desktop.
Template Literals in ES6 vs. conventional String Concatenation approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
function sum(x, y) { | |
/** | |
Using the conventional plus (+) opertior | |
for concatenating strings. | |
*/ | |
console.log('Convemtional way: ') | |
console.log('Values of x and y are: ' + x + ' and ' + y + ' respectively.'); | |
console.log('Sum: ' + (x + y)); | |
/** | |
Using Template Literal approach in ES6 | |
IMPORTANT NOTE: The output line where String Literals (${...}) are used, are enclosed in Back-ticks (``) or Acutes | |
not in single code ('') like we have in conventional method of string concatenation paaroach. | |
conosle.log(`Values...${x}...`); | |
Template Literals won't replace with single quotes. | |
*/ | |
console.log('ES6 way: ') | |
console.log(`Values of x and y are ${x} and ${y} respectively`); | |
console.log(`Sum: ${x + y}`); | |
} | |
</script> | |
</head> | |
<body> | |
<button onclick="javascript: sum(15, 20);">Show in console</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment