Aspects of `const` & `let` in ES6+

function varFunc() {
var count = 1;
if (true) {
var x = 2; // same variable!
console.log(x); // 2
}
console.log(x); // 2
}
function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log(x); // 2
}
console.log(x); // 1
}
> var x = 0;
undefined
> let x = 0;
SyntaxError: Identifier 'x' has already been declared
> let x = 0;
undefined
> var x = 0;
SyntaxError: Identifier ‘x’ has already been declared
> let x = 0
undefined
> let x= 1
SyntaxError: Identifier ‘x’ has already been declared
> x = 5
5
> const pi = 3.14;
> typeof(pi)
'number'
> pi = 4;
> TypeError: Assignment to constant variable.
> const fruits = [“apple”,”mango”];> fruits.push("orange")> fruits
[ 'apple', 'mango', 'orange' ]
> fruits = [“banana”]
TypeError: Assignment to constant variable.
const car = function(){ this.model; this.type;}
> car.model="model s"
'model s'
> car.type="sports"
'sports'
> car.year = "2017"
>car
{ [Function: car] model: 'model s', type: 'sports', year: '2017' }
>car = function(){ this.model}
TypeError: Assignment to constant variable.

--

--

--

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring https://mentorcruise.com/mentor/abhishekkoserwal/

Love podcasts or audiobooks? Learn on the go with our new app.

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
Abhishek koserwal

Abhishek koserwal

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring https://mentorcruise.com/mentor/abhishekkoserwal/

More from Medium

JavaScript ES6 Spread Operator

JavaScript objects in depth.

JS-11 — JavaScript for Programmers, Chapter9, Part1

Asynchronous JavaScript