let,const & old var revisited
yogeshwaran
Posted on March 29, 2024
Every application in real life we need some temporary storage to save, process and get output from it. They are called variables.
A variable is a “named storage” for data. We can use variables to store cart, visitor count, and other data.
Let
We can store variable data in let by using let keyword
let str1; // variable created
str1 = "Message" // Value assigned to a variabled
// we can assign while declaring a variable
let str2 = "message";
It is possible to declare multiple let variables in a single line but it is recommended to keep in each line for better readability
// Not recommended
let newStr = "content1", str2 = "Content2", str3= "Content3";
// Best practice
let newStr = "content1";
let str2 = "content2";
let str3 = "content3";
Variable naming in LET
There are some limitations on variable names in JavaScript:
- The name must contain only letters, digits, or the symbols $ and _.
- The first character must not be a digit.
- We can't use reserve keywords for naming variables like let, class, return, and function, and more
- Case sensitive : gender and GENDER are considered as different
// Valid names
let userGender;
let test123;
// Invalid
let user-gender; // Hypens not allowed
let 1a; // Should not start with number
Const
To store constant values which can't be modified by any operations, we use CONST keyword.
const gender = "male";
gender = "female" //error can't reassign the const
Naming CONST
It is a best practice across industry to name CONSTANTS in full caps with underscores.
const DATE_OF_BIRTH = 18/02/1998;
old VAR
VAR is the old way of declaring variables in javascript which is similar to LET. It's generally not used in modern javascript due to its different limitations and behavior.
var has no BLOCK scope
Variables declared with var are not either has block or function scope.
// var lives even after if blocks
if (true) {
var isOpen = true; // use "var" instead of "let"
}
alert(isOpen); // true;
// Rewrite with let
if (true) {
let isOpen = true; // use "var" instead of "let"
}
alert(isOpen); // Reference error
Redeclaration is possible with var
We can redeclare any variables using var and it won't throw any erros.
var name = 'kate';
var name = 'winslet';
console.log(name); // winslet
Hoisting with 'var'
There is a famous interview question Explain hoisting in javascript . Variables declared with var and moved to top by default. This behavior of moving up is known as hoisting
// Value assigned before creating variable.
function sayHi() {
phrase = "Hi";
alert(phrase);
var phrase;
}
sayHi(); // Hi
But we should understand that only declarations are hoisted not assignment.
function sayHi() {
alert(phrase);
var phrase = "Hello";
}
sayHi(); // undefined
To mitigate the limitations of scoping in var. developers used concept called IIFE - Immediately invoked function expression.
Summary
Let - used to declare varying variables
CONST - used to store constant variables
Var - Old textbook way Don't use now
Hope we are refreshing good parts of javascript. I will come up with more interview part in upcoming parts. Subscribe and follow up for upcoming posts.
Posted on March 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.