JavaScript Refresher

let and const

var used to be the de facto variable declaration keyword used in JavaScript till ES6. Now, it’s encouraged to use let and const. First, let’s go through what var does.

console.log(test);//undefined
var test = "Hi";
var test = "Hi"
var no = 5
if (no == 5) {
var test = 0;
}
console.log(test);//0

Arrow functions

Arrow functions are the norm nowadays in JavaScript. A normal JS function looks like the below snippet.

function myDobuleFun(no){
return no * 2;
}
const myDoubleFun = (no) => {
return no * 2;
}
const myDobuleFun = no => no * 2;

Exports and Imports(Modules)

We can write modular code with ES6. The way to do it is through exports and imports. Let’s look at export and import as a few examples.

//test.js
const test = 10;
export default-test;
//app.js
import test from './test.js'
import t from './test.js'
//util.js
export const pi = 3.14;
export const e = 2.718;
//app.js
import {pi} from './util.js'
import {e} from './util.js'
import {pi as math_pi} from './util.js'
import * as bundled from './util.js'

Spread and Rest Operators

Both are represented by .

let no = [1, 2, 3];
let newNo = [...no, 4];//[1, 2, 3, 4]
let newNo = [no, 4];//[[1, 2, 3], 4]
const filter = (...args) => {
return args.filter(e1 => e1 === 1);
}

Destructuring

This is used to extract array elements or object properties and store them in variables.

[a, b] = ['Hi', 'World'];
console.log(a); //Hi
console.log(b); //World
{name} = {name: 'John', age: 20};
console.log(name)//John
console.log(age)//undefined

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store