- A variable is a named container used to store a value in JavaScript
- Variables are created using
var,let, orconst - JavaScript is loosely typed, so a variable's type is set automatically
- Every value in JavaScript belongs to a data type, such as string or number
- Data types are grouped into primitive types and non-primitive (reference) types
- Choosing the right keyword and type keeps code predictable and bug-free
What Are Variables in JavaScript?
A variable in JavaScript works like a labeled box where you keep a piece of information so you can use it again later in your code. Instead of typing the same value over and over, you store it once inside a variable and simply call its name whenever you need it.
let or function.
Declaring a Variable Example
<script>
let name = "Shivam";
console.log("Hello " + name);
</script>
Hello Shivam
Why Learn Variables and Data Types?
Foundation
Every script, function, and web application begins with storing and organizing data.
Cleaner Code
Reusable variables cut down repetition and reduce mistakes.
Faster Debugging
Knowing the exact type of a value helps you spot errors early.
Works Everywhere
The same rules apply in the browser and in Node.js.
JavaScript Data Types
JavaScript data types fall into two broad groups: primitive types and non-primitive (reference) types.
- String – text wrapped in quotes, such as "Hello"
- Number – whole numbers and decimals, such as 25 or 3.14
- Boolean – either true or false
- Undefined – a variable declared but not yet given a value
- Null – an intentional empty value
- Object – a group of related key-value pairs
- Array – an ordered list of values
Data Type Example
<script>
let age = 25;
let city = "Delhi";
let isStudent = true;
console.log(typeof age, typeof city, typeof isStudent);
</script>
number string boolean
What You Will Learn
Basics
How to declare, name, and initialize variables correctly.
Data Types
Strings, numbers, booleans, and the other types JavaScript supports.
Scope
Where a variable can and cannot be accessed.
var / let / const
The real, practical difference between all three.