JavaScript Variables
Variables are used to store data values in JavaScript. They work like containers that hold information which can be used later in the program.
Variables make programs dynamic because values can be stored, updated, and reused whenever needed.
Why Variables are Important?
- Store user data
- Perform calculations
- Reuse values multiple times
- Make programs interactive
- Help organize data properly
How to Create Variables
JavaScript provides three keywords to create variables:
let const var
Using let Keyword
The let keyword is used when variable values may change later.
let name = "Ali"; console.log(name);
Output
Ali
Explanation
- let creates a variable.
- name is the variable name.
- "Ali" is the stored value.
- console.log() displays output in the console.
Changing Variable Values
Variables created with let can be updated.
let city = "Delhi"; city = "Mumbai"; console.log(city);
Output
Mumbai
Explanation
- The variable initially stores "Delhi".
- Later the value changes to "Mumbai".
- JavaScript prints the updated value.
Using const Keyword
const is used for fixed values that should not change.
const country = "India"; console.log(country);
Output
India
Important Rule of const
A const variable cannot be reassigned.
const pi = 3.14; pi = 3.1415;
Explanation
- const creates a constant variable.
- Its value remains fixed.
- Changing the value produces an error.
Using var Keyword
var is the older way of declaring variables in JavaScript. Modern JavaScript mainly uses let and const.
var marks = 90; console.log(marks);
Output
90
Difference Between let, const, and var
- let → Value can change
- const → Value cannot change
- var → Older variable declaration method
Variable Naming Rules
- Names can contain letters, numbers, _ and $.
- Names cannot start with numbers.
- Spaces are not allowed.
- Keywords cannot be used as variable names.
- Variable names are case-sensitive.
Valid Variable Names
let age = 20; let studentName = "Rahul"; let total_marks = 500; let $price = 99;
Invalid Variable Names
let 1name = "Ali"; let user name = "Rahul"; let let = 50;
Explanation
- Variables cannot start with numbers.
- Spaces are not allowed.
- Reserved keywords cannot be used.
Case Sensitivity
JavaScript treats uppercase and lowercase letters differently.
let userName = "Ali"; let username = "Ahmed"; console.log(userName); console.log(username);
Output
Ali Ahmed
Explanation
- userName and username are different variables.
- JavaScript is case-sensitive.
Declaring Multiple Variables
let name = "Ali",
age = 20,
city = "Delhi";
console.log(name);
console.log(age);
console.log(city);
Output
Ali 20 Delhi
JavaScript Variable Scope
Scope defines where variables can be accessed.
Block Scope
Variables declared with let and const are block-scoped.
{
let message = "Hello";
console.log(message);
}
Output
Hello
Global Scope
Variables declared outside blocks are globally accessible.
let website = "CodeVyro"; console.log(website);
Output
CodeVyro
Dynamic Typing in JavaScript
JavaScript is dynamically typed, meaning variables can store different data types.
let value = 100; value = "JavaScript"; console.log(value);
Output
JavaScript
Complete Real Life Example
The following example shows how variables are used in a student management system.
let studentName = "Rahul";
let studentAge = 20;
let studentMarks = 85;
let passed = true;
console.log("Student Name: " + studentName);
console.log("Student Age: " + studentAge);
console.log("Student Marks: " + studentMarks);
console.log("Passed: " + passed);
Output
Student Name: Rahul Student Age: 20 Student Marks: 85 Passed: true
Detailed Explanation of Example
- studentName stores the student's name.
- studentAge stores the age value.
- studentMarks stores marks obtained by the student.
- passed stores a boolean value (true or false).
- console.log() prints all values to the console.
- The + operator combines text and variables together.
Best Practices for Variables
- Use meaningful variable names.
- Prefer let and const instead of var.
- Use const whenever values should not change.
- Keep naming style consistent.
- Write readable and clean code.
Common Naming Styles
Camel Case
let studentName;
Snake Case
let student_name;
Important Notes
- Variables store program data.
- JavaScript variables are case-sensitive.
- let allows value changes.
- const creates fixed values.
- Good variable names improve readability.
Summary
- Variables are containers for storing data.
- JavaScript uses let, const, and var.
- let variables can change.
- const variables remain fixed.
- Variables follow naming rules.
- JavaScript supports dynamic typing.
- Meaningful variable names improve code quality.