Advanced JavaScript: Working with Var, Let, and Const Variables | Modern JS | ES6 (Hindi/Urdu)

Posted by






02 – Var, Let And Const Variables In JavaScript

Var, Let And Const Variables In JavaScript

JavaScript is a versatile programming language that provides different ways to declare variables. In this article, we will discuss the usage of var, let, and const keywords to define variables in JavaScript.

Var Keyword

The var keyword has been used to declare variables in JavaScript for a long time. Variables declared with var are function-scoped, meaning they are only accessible within the function in which they are declared. If a variable is declared with var outside of any function, it becomes a global variable, accessible throughout the entire JavaScript program.

Let Keyword

The let keyword was introduced in ES6 and it allows us to declare block-scoped variables. This means that variables declared with let are only accessible within the block in which they are defined, such as within a for loop or an if statement block. Using let can help prevent variable hoisting and reduce the risk of accidental variable re-declaration.

Const Keyword

The const keyword is also introduced in ES6 and it is used to declare constants in JavaScript. Once a variable is declared with const, its value cannot be changed or reassigned. This ensures that the variable remains immutable throughout the program. However, it is important to note that const only makes the variable itself immutable and not its contents. For example, if a const variable is an object, its properties can still be modified.

Example:

Here’s an example demonstrating the usage of var, let, and const keywords:

“`javascript
// Using var keyword
function varExample() {
if (true) {
var message = “Hello, World!”;
}
console.log(message); // Output: Hello, World!
}

// Using let keyword
function letExample() {
if (true) {
let message = “Hello, World!”;
console.log(message); // Output: Hello, World!
}
console.log(message); // Error: message is not defined
}

// Using const keyword
const PI = 3.14;
PI = 3.14159; // Error: Assignment to constant variable
“`

By using var, let, and const keywords appropriately, we can ensure better control over variable scoping and immutability in our JavaScript programs.

Conclusion

In this article, we discussed the var, let, and const keywords in JavaScript and their implications for variable declaration and usage. It is important to understand the differences between these keywords in order to write more robust and maintainable JavaScript code.


0 0 votes
Article Rating
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Deepali Biradar
1 year ago

Very very nice video

Shona Gaming FF
1 year ago

😍😍😍

Code and Traveling
1 year ago

Sir Jase k AP ne kaha k var if k scop me global scop he but function k scop me jb ham var ko initialize karte hen to wo function k scop se bahir call nhi hosakta please clear this point

Nika Raju
1 year ago

bht awlaaaaaa

Damayanti Gulave
1 year ago

sir pls angular ki remaining series complete kro na,rxjs operator,http methods,routing guards,lazyloading,

Bilal Shoukat
1 year ago

nice sir🥰🥰🥰🥰

Raikoti Sunny
1 year ago

Sir angular ki series khatam hai ?

Lovely Music
1 year ago

Sir any idea about firebase course for web?