Variables are the building blocks of any programming language. They store data that your code can use and manipulate.
JavaScript is a dynamically typed language, meaning you donโt need to declare the data type of a variable explicitly โ it is determined at runtime.
A variable is a container for storing data values.
let message = "Hello, world!";
let
is the keyword.message
is the variable name."Hello, world!"
is the assigned value.JavaScript provides three ways to declare variables:
Keyword | Scope | Reassignment | Hoisting | Use Case |
---|---|---|---|---|
var |
Function-scoped | โ Yes | โ
Hoisted (initialized as undefined ) |
Legacy code |
let |
Block-scoped | โ Yes | โ Hoisted (not initialized) | Recommended for variables that change |
const |
Block-scoped | โ No | โ Hoisted (not initialized) | Recommended for constants |
var name = "Ajay";
let age = 23;
const country = "India";
JavaScript has two main categories of types:
Type | Example | Description |
---|---|---|
String |
"Hello" |
Textual data |
Number |
42 , 3.14 |
Integer or floating point numbers |
Boolean |
true , false |
Logical true or false |
Null |
null |
Intentionally empty value |
Undefined |
undefined |
Declared but not assigned |
Symbol |
Symbol("id") |
Unique and immutable identifier |
BigInt |
123n |
Large integers |
Object
Array
Function
Date
, RegExp
, etc.console.log(typeof 123); // "number"
console.log(typeof "JS"); // "string"
console.log(typeof null); // "object" (quirk in JS)
console.log(typeof undefined); // "undefined"
flowchart TD
A[Declare Variable] --> B[Assign Value]
B --> C{Which Keyword?}
C --> D[var]
C --> E[let]
C --> F[const]
D --> G[Function Scope]
E --> H[Block Scope]
F --> I[Block Scope, Immutable]
let
for variables you plan to reassign.const
by default unless reassignment is needed.var
unless working in legacy code.userAge
, isLoggedIn
).1. What is the difference between var, let, and const?
var
: Function-scoped, hoisted, can be redeclared.let
: Block-scoped, hoisted but not initialized.const
: Block-scoped, cannot be reassigned.2. What are primitive and non-primitive data types?
3. Why is typeof null an object?
Itโs a legacy bug in JavaScriptโs original implementation. Technically, null
is a primitive.
4. Can we change the value of a const variable?
No, const
variables cannot be reassigned. However, for objects and arrays, their contents can be mutated.
const person = { name: "Ajay" };
person.name = "Dhangar"; // โ
Allowed
person = { name: "Someone" }; // โ Error
5. What does dynamically typed mean in JavaScript?
It means data types are assigned at runtime, and the same variable can hold values of different types during execution.
Visit CodeHarborHub for projects, interactive demos, and more JavaScript practice.
var
, let
, and const
to declare variables.let
and const
for modern, safe coding practices.๐ฏ Mastering variables and data types is crucial to writing clean, bug-free JavaScript code.
Continue your learning in the next section: Operators in JavaScript