Variables in JavaScript
Introduction to Variables in JavaScript
Variables in JavaScript are one of the most important concepts for beginners. In this JavaScript tutorial for beginners, you will learn how variables in JavaScript are used to store data and build real applications. Understanding variables in JavaScript helps you write dynamic and reusable code.
If you want to continue learning step by step, you can click here for free courses and explore the complete JavaScript tutorial.
What are Variables in JavaScript
Variables in JavaScript are containers used to store values such as numbers, text, or boolean data. These values can be updated and reused throughout your program.
Example of Variables in JavaScript
let age = 22;console.log(name);
console.log(age);
In this example, variables in JavaScript are used to store a name and age.
How to Declare Variables in JavaScript
There are three ways to declare variables in JavaScript: var, let, and const.
Using var in JavaScript
console.log(city);
The var keyword is used in older JavaScript and has function scope. It can also be re-declared.
Using let in JavaScript
console.log(score);
The let keyword is used in modern JavaScript. It has block scope and is safer than var.
Using const in JavaScript
console.log(pi);
The const keyword is used when the value should not change after assignment.
Examples of Variables in JavaScript
Storing Different Data Types
let marks = 85;
let isPassed = true;console.log(userName);
console.log(marks);
console.log(isPassed);
Updating Variables
count = 20;console.log(count);
Performing Operations Using Variables
let b = 10;
let sum = a + b;console.log(sum);
Rules for Naming Variables in JavaScript
Start with Valid Characters
Variable names must start with a letter, underscore, or dollar sign.
Avoid Reserved Keywords
// let let = 10;
You cannot use JavaScript reserved keywords as variable names.
Case Sensitivity
let Name = “Ravi”;console.log(name);
console.log(Name);
Variables in JavaScript are case-sensitive, so name and Name are different.
Why Variables in JavaScript are Important
Variables in JavaScript help you store, update, and manage data efficiently. They are essential for building interactive websites, handling user input, and performing calculations.
Conclusion
Variables in JavaScript are the foundation of programming. By learning how to declare and use variables in JavaScript, beginners can start writing real programs and move toward advanced topics.
FAQs
What are variables in JavaScript
Variables in JavaScript are used to store data values that can be reused and updated in a program.
What is the difference between var let and const in JavaScript
var is function scoped, let is block scoped, and const is used for fixed values that cannot be changed.
Why should I learn variables in JavaScript
Learning variables in JavaScript helps you build logic, store data, and create dynamic applications.



