Primitive Datatypes
- Number
- Represents both integer and floating-point numbers.
- Example:
let integer = 42; let float = 3.14;
- Special values: `Infinity`, `-Infinity`, `NaN` (Not-a-Number).
- String
- Represents sequences of characters.
- Can be defined using single quotes (
'
), double quotes ("
), or backticks (`
) for template literals. - Example:
let singleQuote = 'Hello';
let doubleQuote = "World";
let templateLiteral = `Hello, ${doubleQuote}!`;
- Boolean
- Represents logical values:
true
andfalse
. - Example:
- Represents logical values:
let isTrue = true; let isFalse = false;
-
Undefined
-
Represents a variable that has been declared but not assigned a value.
-
Example:
let x; console.log(x); // undefined
-
-
Null
-
Represents the intentional absence of any object value.
-
Example:
let y = null;
-
-
Symbol (ES6)
-
Represents a unique and immutable value often used as object keys.
-
Example:
let sym = Symbol('description');
-
-
BigInt (ES11)
-
Represents whole numbers larger than the maximum safe integer (
Number.MAX_SAFE_INTEGER
). -
Example:
let bigInt = 1234567890123456789012345678901234567890n;
-
Composite (Complex) Datatypes
-
Object
-
A collection of properties, where each property is a key-value pair.
-
Keys can be strings or symbols, and values can be any datatype.
-
If Keys are number data type then value is implicitly converted into strings.
-
Example:
let obj = { name: 'Alice', age: 30, greet: function() { console.log('Hello!'); } };
-
-
Array
-
A list-like collection of elements, accessible via numerical indices.
-
Example:
let arr = [1, 2, 3, 'four', { five: 5 }];
-
Special Datatypes
- Functions
-
A callable object that encapsulates code to be executed.
-
Functions can be defined in various ways (function declarations, expressions, arrow functions).
-
Example:
function greet() { return 'Hello!'; } let greetArrow = () => 'Hello!';
-
Type Conversion and Coercion
-
Implicit Type Coercion: JavaScript automatically converts values from one datatype to another in certain operations.
-
Example:
console.log('5' + 5); // '55' (string concatenation) console.log('5' - 2); // 3 (numeric subtraction)
-
-
Explicit Type Conversion: Manually converting from one datatype to another using functions.
-
Example:
let num = Number('123'); // Converts string to number let str = String(123); // Converts number to string
-