JavaScript Variables (var, let, const)
What is a Variable?
Variables are fundamental to all programming languages. They serve as containers for storing various types of data, such as text strings, numbers, and more. These containers allow you to set, update, and access data whenever needed. Simply put, variables are symbolic names representing values.
To create a variable, you use the `var` keyword, and the assignment operator (`=`) assigns a value to the variable. For example: `var variableName = value;`
Naming Conventions for JavaScript Variables
Follow these rules when naming a JavaScript variable:
– The name must start with a letter, underscore (_), or dollar sign ($).
– It cannot start with a number.
– It can only contain alphanumeric characters (A-z, 0-9) and underscores.
– Spaces are not allowed.
– It cannot be a JavaScript keyword or reserved word.
Declaration of JavaScript Variables
There are three ways to declare a JavaScript variable:
1. Using `var`:
var name = “Aayesha Khan”;
var age = 24;
var isGood = true;
// Declaring a Variable
var userName;
// Assigning a value
userName = “Aayesha Khan”;
2. Using `let` and `const` (ES6):
// Declaring variables
let characterName = “Harry Potter”;
let characterAge = 11;
let isCharacterStudent = true;
// Declaring a constant
const PI = 3.14;
console.log(PI); // 3.14
// Trying to reassign (this will result in an error)
PI = 10; // Error
Unlike `var`, which declares function-scoped variables, both `let` and `const` declare variables scoped at the block level (`{}`). Block scoping means that a new scope is created within a pair of curly brackets {}.
Differences Between `var`, `let`, and `const`
1. Var:
– Traditional way of declaring variables.
– Function-scoped, not block-scoped.
– Can be reassigned and updated.
– Hoisted to the top of their scope during execution.
var score = 80;
function updateScore() {
var score = 90;
// Function-scoped
}
2. Let:
– Introduced in ES6, offers block-scoping.
– Can be reassigned, but not re-declared in the same scope.
– Provides more predictable behavior compared to `var`.
let count = 10;
if (true) {
let count = 20;
// Block-scoped
}
3. Const:
– Also introduced in ES6, block-scoped.
– Cannot be reassigned once declared.
– Provides a constant reference to the value.
const PI = 3.14;
PI = 4; // Error: Cannot reassign a constant variable.
When to Use Each?
– Use `var` if you’re working with older codebases.
– Prefer `let` when you need a variable that can be reassigned.
– Use `const` when the variable’s value should remain constant.
Course Video in Hindi
Variables are named containers that store data values in JavaScript, allowing developers to manipulate and use data within their programs.
You can declare variables using var, let, or const. Example:
let x = 5;
const y = 10;
var z = 15;
var is function-scoped and can be redeclared.
- let is block-scoped and cannot be redeclared in the same scope.
- const is block-scoped and its value cannot be reassigned.
Yes, you can declare a variable without a value. It will have the value undefined by default. Example:
let myVariable;
console.log(myVariable); // undefined
- Variable names must begin with a letter, _, or $.
- They cannot contain spaces or special characters.
- JavaScript is case-sensitive (myVar and myvar are different).
No, reserved keywords like if, else, and return cannot be used as variable names.
You can reassign values to variables declared with var or let, but not with const. Example:
let num = 10;
num = 20; // Allowed
- Global variables: Declared outside functions and accessible anywhere in the code.
- Local variables: Declared within a function and only accessible within that function.
Using an undeclared variable automatically creates it as a global variable (in non-strict mode), which is not recommended.
Hoisting is JavaScript’s default behavior of moving declarations to the top of their scope during execution. However, var variables are initialized as undefined, while let and const are not initialized.
In JavaScript, you can declare variables using var, let, or const. For example:
javascript
Copy code
let name = “John”;
const age = 25;
var isActive = true;
You can learn more about declaring variables in our Free JavaScript variables tutorial for beginners.
The main differences between var, let, and const in JavaScript are related to scope, hoisting, and reassignment.
- var is function-scoped and can be redeclared.
- let is block-scoped and can be reassigned but not redeclared in the same scope.
- const is block-scoped and cannot be reassigned or redeclared.
For more details, check our Differences between var, let, and const in JavaScript tutorial.
We offer a Free JavaScript variables tutorial for beginners, where you will learn everything you need to know about declaring and working with variables in JavaScript, including practical examples.
Yes, our website provides a JavaScript variables free course video, which walks you through the concepts of variables in JavaScript, such as how to declare them and the differences between var, let, and const.