In JavaScript, data types are divided into Primitives and Objects.
These are stored by value in the Stack. Once created, they cannot be changed (you can only reassign a new value to the variable).
Infinity and NaN (Not a Number).' ', " ", or ` `.true or false.Stored in the Heap. These include:
{ }[ ]function() {}typeof OperatorThe typeof operator is used to check the type of a value. However, it has a famous “bug” that you must remember for interviews:
JavaScript
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof { name: "A" }); // "object"
// THE WEIRD ONES:
console.log(typeof null); // "object" (This is a historical bug in JS!)
console.log(typeof function(){}); // "function" (Technically an object, but returns function)
JavaScript is a loosely typed language. This means it automatically converts types when needed.
When you use the + operator with a string, JS converts the other value to a string.
JavaScript
console.log("5" + 2); // "52" (Number 2 is coerced to string)
console.log("5" + true); // "5true"
Other mathematical operators (-, *, /, %) convert strings to numbers.
JavaScript
console.log("10" - 2); // 8
console.log("10" * "3"); // 30
console.log("10" / "two");// NaN (Not a Number)
In a conditional (like an if statement), JS coerces values to Booleans.
Falsy Values (There are only 6):
false0 (and -0, 0n)"" (Empty string)nullundefinedNaNEverything else is Truthy, including empty arrays [] and empty objects {}.
==) vs. Triple Equals (===)This is the most common source of bugs.
== (Abstract Equality): Compares values after performing type coercion.=== (Strict Equality): Compares both value and type without coercion.Example:
JavaScript
console.log(5 == "5"); // true (Coerces string to number)
console.log(5 === "5"); // false (Different types)
console.log(null == undefined); // true
console.log(null === undefined); // false
Pro Tip: Always use === to avoid unexpected bugs.
Sometimes you want to convert types manually to keep your code predictable.
JavaScript
// To String
String(123); // "123"
(123).toString();
// To Number
Number("123"); // 123
parseInt("123.45px"); // 123
parseFloat("123.45px"); // 123.45
// To Boolean
Boolean(1); // true
Boolean(0); // false
!!(1); // true (Short way using "double bang")
A data type defines:
Example:
int age = 21; → int is the data typeUsed to store whole numbers.
| Type | Typical Size |
|---|---|
| byte | 1 byte |
| short | 2 bytes |
| int | 4 bytes |
| long | 8 bytes |
Examples:
int count = 10;long population = 1400000000;Used for decimal values.
| Type | Precision |
|---|---|
| float | Single precision |
| double | Double precision |
Example:
double price = 99.99;Stores a single character.
Example:
char grade = 'A';Stores true or false values.
Example:
boolean isActive = true;Example:
int marks[5]
Example:
"Hello World"Groups different data types.
Example:
struct Student {
int id;
char name[20];
};
Shares memory among variables.
Defines named constants.
Example:
enum Status { ACTIVE, INACTIVE };
Blueprint of objects (OOP).
Converting one data type into another.
Two types:
Automatically done by the compiler or interpreter.
Example:
int → floatfloat → doubleExample:
int a = 10;
double b = a;
Manually converting one type to another.
Example:
double x = 9.8;
int y = (int)x;
Example:
int → doubleExample:
double → intAutomatic conversion of data types during expression evaluation, often seen in loosely-typed languages.
Example:
"5" + 2 = "52"
"5" - 2 = 3
(Common in JavaScript, Python to a limited extent)
| Aspect | Type Conversion | Type Coercion |
|---|---|---|
| Manual | Yes | No |
| Automatic | Sometimes | Yes |
| Control | Programmer | Language |
| Risk | Medium | High |
Examples:
Example:
Example:
Example:
Ensures operations are performed on compatible data types.
Benefits:
0, 1, true, false)== vs ===)