Template Strings in ES6 provide us with an alternative to string concatenation. They also
allow us to insert variables into a string. that’s make quite easy for String Concatenation in ES6
But with a Template Strings, we can create one string and insert the variable values by surrounding them with ${ }:
Example of Traditional Strings Concatenation:
console.log(lastName + “, ” + firstName + ” ” + middleName )
Example of Concatenation with Template Strings in ES6
console.log(`${lastName}, ${firstName} ${middleName}`)
Default parameters in ES6 ?
ES6 also provides default parameters if there is no argument
Syntax:
function functionname(param1 = defaultValueone, ..., nthpara = nth defult value) { ... }
Example:
function Sun(a, b = 10) {
return a + b
}
multiply(10, 2) // output will be 12
multiply(10) // output will be 20
multiply(10, undefined) // output will be 20
What is Transpiling ES6 ?
Through Transpiling ES6 we can easily convert ES6 to ES5
currently most of the browser support ES6. but if does not support then Transpiling ES6 is used to convert ES6 to ES5
Example of ES6 to ES5
const add = (x=5, y=10) => console.log(x+y);
Convert ES6 to ES5 through Transpiling ES6
Transpiling ES6 code of Arrow add function
"use strict";
var add = function add() {
var x = arguments.length <= 0 || arguments[0] === undefined ?
5 : arguments[0];
var y = arguments.length <= 1 || arguments[1] === undefined ?
10 : arguments[1];
return console.log(x + y);
};
In the above example the transpiler added a “use strict” declaration to run in strict mode. The variables x
and y are defaulted using the arguments array, a technique you may be familiar with.
we can transpile JavaScript directly in the browser using the inline Babel transpiler.
we just have to include the browser.js file, and any other scripts with type=”text/babel” will be converted (even though Babel 6 is the current version of Babel, only the CDN for
Babel 5 will work)
Example
<script
src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js">
</script>
<script src="script.js" type="text/babel">
</script>