Differences Between Const, Var and Let

My notes about const, var, and let. Not a guide. Just notes.

Var #

var myName = 'Yasin';
console.log(myName);

You will see Yasin output in console.

var myName = 'Yasin';
myName = 'Dmitry';
console.log(myName);

You will see Dmitry output in console because we changed value of the variable.

var myName = 'Yasin';
var myName = 'Dmitry';
console.log(myName);

You will see Dmitry output in console because. You can redeclarate.

var myName = 'Yasin';
function logMyName() {
console.log(myName);
};
logMyName();

In this example you will see Yasin output in console. You can reach variable even you in a scope.

var myName = 'Yasin';
function logMyName() {
var myName = 'Dmitry';
console.log(myName);
};
logMyName();

In this example you will see Dmitry output in console. You can reach variable in a scope and change it value.


Let #

let myName = 'Yasin';
let myName = 'Dmitry';
console.log(myName);

Redeclaration is not possible. This will throw an error.


Const #

const myName = 'Yasin';
myName = 'Dmitry';
console.log(myName);

Can't change the value of the const. This will throw an error.

Previous Post

JSX Arrays and Forms (React Notes - 2)

My ReactJS learning journey. I take these notes for myself.

Read article
Next Post

JavaScript ES6 Classes

Classes are a template for creating objects. They encapsulate data with code to work on that data.

Read article