From vanilla to red velvet

Faiza A. Khan
Mar 26 · 2 min read

Quick transformation from vanilla JavaScript to ES6

Template literals

You have used concatenation in string, there wer some limitations.
1. You can not use multi line string.

2. If you have to display a long chain of string with different variable then your life is a mess as you put + in between variables or hard-quote string. for example

Instead we use template literals with back-ticks ` and put every variable with dollar $ sign and enclose it in curly brackets.

Get familiar with let and const

understand some basic differences
1. let can be reassigned and const can not
2. Both let and const are block scope
3. If you do not want any variable to be used out side the block use let

var x = 5;
{ let x=7;} // x =7 inside the block
// x = 5 outside the block

Use Arrow Functions

legacy functions are simpler but arrow functions are
1. much more concise
2. good for other arrays methods.
3. automatically bind “this” to code context
Now see the difference in syntax (pretty easy by the way)

What is happening
* function keyword is not needed instead we add const and variable name.
* start with parentheses with parameters if any and followed by fat arrow =>
* if there is only one line returning you can omit return keyword as well curly braces

some arrays function (believe me it will save your life)

map(), find(),reduce(),filter() methods in arrays can save bulk of time writing your own logic. Let me explain map and filter

default parameters

With older version when function needs parameter you have to provide when you call that function for e.g

function add(a=6,b=5){
return a+b
}
add(2,3);//correct adds 2+3
add(2);//correct adds 2+5
add();//correct add default values 6+5

More From Medium

Also tagged ES6

Top on Medium

Mar 25 · 22 min read

27K

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade