JavaScript Variables & Data Types Summary
1. Variables: Containers for Data
Variables are used to store data values. In modern JavaScript (ES6+), there are three main
keywords for declaring a variable:
Keyword
Scope
Reassignment
Best Use Case
let
Block Scope ({})
Allowed
When the variable's value needs to change (e.g.,
counters, loops).
const
Block Scope ({}) Not Allowed
When the variable's value should not change
(immutable reference). This is the default choice.
var
Function Scope
Avoid in new code due to hoisting and scope
issues.
Allowed
Block Scope: A variable defined inside a block of code (like an if statement or for loop, indicated
by {}) is only accessible within that block.
2. Data Types: What Kind of Data?
JavaScript data types describe the kind of value a variable hold. They are divided into two main
categories: Primitive and Non-Primitive (Reference).
A. Primitive Data Types (7 Types)
These are values that are immutable (cannot be changed directly) and hold a single value.
Type
Description
Example
string
Textual data, must be enclosed in
quotes.
number
Numeric values (integers and
floating-point numbers).
10, 3.14159, -5
bigint
For integer numbers larger than
253 - 1.
12345678901234567890n
boolean
Represents logical entities, either
true or false.
true, false
undefined
The variable has been declared but
has not yet been assigned a value.
let x; (x is undefined)
"Hello World", 'JS Summary', `template`
1
Type
Description
Example
null
Represents the intentional absence
of any object value.
let y = null;
symbol
Used to create unique identifiers
(rarely used in basic notes).
Symbol('id')
Key Difference: null is an assigned value that means "no value," while undefined means a value
has not been assigned yet.
B. Non-Primitive (Reference) Data Types (1 Type)
These are values that are mutable (can be changed) and do not hold the value directly, but rather a
reference to a location in memory.
Type
Description
Example
object
A collection of properties, complex
data structures.
const user = { name: "Alice", age: 30 };
The object type is the parent of all complex data structures, including:
•
•
Arrays: Ordered lists of values ([1, 'a', true])
Functions: Callable objects that execute a block of code.
3. Checking Data Types
You can use the typeof operator to check the data type of a variable.
JavaScript
typeof 42;
typeof "text";
typeof true;
typeof undefined;
typeof null;
typeof {a: 1};
typeof [1, 2, 3];
// "number"
// "string"
// "boolean"
// "undefined"
// "object" <-- A historical bug in JavaScript, often called
the typeof null bug"
// "object"
// "object" (Arrays are a type of object)
2
4.
Type Coercion
Type coercion is JavaScript's automatic or implicit conversion of values from one data type to
another (e.g., a number to a string).
There are two main types of coercion:
A. Implicit Coercion (Automatic)
This happens automatically when you use operators that require operands of a specific type.
Scenario
Example
Result
Explanation
String
Coercion
'5' + 1
'51' (string)
The + operator performs string concatenation if either
operand is a string. The number 1 is coerced to the
string '1'.
Number
Coercion
'5' - 1
4 (number)
The - operator only works with numbers, so the string
'5' is coerced to the number 5.
Boolean
Coercion
if ('hello')
true
{ ... }
In logical contexts, non-empty strings, non-zero
numbers, and objects are coerced to true (truthy
values).
Falsy Values: The following values coerce to false in a boolean context: false, 0 (zero), ""
(empty string), null, undefined, and NaN. Everything else is truthy.
B. Explicit Coercion (Manual)
This happens when you intentionally convert the type using built-in JavaScript methods.
Conversion
Method
Example
Result
To String
String() or toString()
String(123)
"123"
To Number
Number() or parseInt()
Number('42')
42
To Boolean
Boolean()
Boolean(0)
false
5. Equality Operators
Type coercion is most often encountered when comparing values.
Operator
Name
Coercion?
Example
Result
==
Loose Equality
YES
'10' == 10
true
===
Strict Equality
NO
'10' === 10
false
Best Practice: Always use Strict Equality (===) to prevent unexpected behavior caused by
implicit type coercion.
3
6. ✨ Operators
Operators are special symbols used to perform operations on operands (values and variables).
A. Arithmetic Operators
Used for mathematical calculations.
Operator
Name
Example
Result
+
Addition
5 + 3
8
-
Subtraction
5 - 3
2
*
Multiplication
5 * 3
15
/
Division
5 / 2
2.5
%
Remainder (Modulo)
5 % 2
1
**
Exponentiation
5 ** 2
25
++
Increment
let a=5; a++
a is now 6
--
Decrement
let b=5; b--
b is now 4
B. Assignment Operators
Used to assign values to variables.
Operator
Name
Shortcut for...
=
Assignment
x = 5
+=
Addition assignment
x += 3 is the same as x = x + 3
-=
Subtraction assignment
x -= 3 is the same as x = x - 3
*=
Multiplication assignment
x *= 3 is the same as x = x * 3
/=
Division assignment
x /= 3 is the same as x = x / 3
4
C. Comparison Operators
Used to compare two values and return a boolean (true or false).
Operator
Name
Coercion?
Example
==
Loose Equality
YES
'10' == 10 (True)
!=
Loose Inequality
YES
'10' != 8 (True)
===
Strict Equality
NO
'10' === 10 (False)
!==
Strict Inequality
NO
'10' !== 10 (True)
>
Greater than
5 > 3 (True)
<
Less than
5 < 3 (False)
>=
Greater than or equal to
<=
Less than or equal to
Recommendation: Always use Strict Equality (===) to avoid unexpected type coercion behavior.
D. Logical Operators
Used to determine the logic between variables or values.
Operator
Name Description
Example
&&
AND Returns true only if both operands are true.
(5 > 3) && (2 < 4) (True)
||
OR
(5 < 3) || (2 > 4) (False)
!
NOT Inverts the boolean value.
Returns false only if both operands are false
!(5 == 5) (False)
5