What is JavaScript?
JavaScript (JS) is a high-level, interpreted, and versatile programming language used to make websites interactive and dynamic. It is one of the three core technologies of web development, alongside HTML and CSS.
- HTML creates the structure of a webpage.
- CSS styles the webpage.
- JavaScript adds behavior and functionality.
Variables
A variable is like a container that stores data.Imagine you have a box labeled Name. Inside that box, you store the value "Dharanidharan".
In JavaScript, variables work the same way—they store information that your program can use later.
Types of Variables
JavaScript provides three ways to declare variables:
let
const
var
1. let
The let keyword is used to declare a variable in JavaScript. It was introduced in ES6 (ECMAScript 2015) and is the recommended way to declare variables whose values may change during the execution of a program.
Syntax:
let variableName = value;
- Reassigning a Variable-A variable declared with let can be reassigned.
- Redeclaring is Not Allowed-You cannot declare the same variable twice in the same block.
Where Do We Use let?
The let keyword is used when the value of a variable may change during the execution of a program.Whenever you expect a variable to store different values at different times, let is the right choice.
Exzample
let y = 10;
console.log(y); // 10
let y = 20; // ❌ Error: Cannot redeclare
y = 20; // ✅ Allowed
console.log(y); // 20
2. const
The const keyword is used to declare a variable whose reference cannot be reassigned after it has been initialized. It was introduced in ES6 (ECMAScript 2015) and is the preferred choice for values that should remain the same throughout the program.
Syntax:
const country = "India";
- const Must Be initialized Unlike let, you cannot declare a const variable without giving it a value.
- both reassignment and redeclaration is not alowed.
where Do We Use Const?
The const keyword is used when a variable's reference should not be reassigned after it is created.Once a value is assigned to a const variable, you cannot assign a different value to that same variable.
Exzample
const z = 10;
z = 20; // ❌ Error: Assignment to constant variable
console.log(z); // 10
3. var(Not Recommended)
The var keyword was traditionally used to declare variables in JavaScript.Variables declared with var are function-scoped and can be re-declared within the same scope without causing an error.
Syntax:
var name = "ABCD";
- Reassigning a Variable-A variable declared with var can be reassigned.
- Redeclaring a Variable-One unique feature of var is that it can be redeclared in the same scope.
Why is var Not Recommended?
- With var, you can declare the same variable multiple times in the same scope. This can accidentally overwrite values.
- var is function-scoped, not block-scoped. This means a variable declared inside an if block or for loop is still accessible outside that block.
- Variables declared with var are hoisted. They exist before the line where they are declared, but their value is undefined until the assignment happens.
Exzample
var x = 10;
var x = 20; // Redeclaration allowed
console.log(x); // 20
Referance:
Top comments (0)