- [[#1-let|1.
let]] - [[#2-var|2.
var]] - [[#3-const|3.
const]] - Data types
- [[#3.
const#1. Primitive Data Types|1. Primitive Data Types]] - [[#3.
const#2. Non-Primitive (Reference) Data Types|2. Non-Primitive (Reference) Data Types]]
- [[#3.
- [[#Data types#Type Conversion in JavaScript|Type Conversion in JavaScript]]
- [[#Type Conversion in JavaScript#Implicit Type Conversion (Type Coercion)|Implicit Type Conversion (Type Coercion)]]
- [[#Type Conversion in JavaScript#2. Explicit Type Conversion|2. Explicit Type Conversion]]
Variables
In JavaScript, there are three ways to declare variables: let, var, and const
let: Use when you need block scope and the variable may change.var: Use in legacy code or when you need function scope (generally less preferred in modern JavaScript).const: Use when the variable should not be reassigned (constants) and you need block scope.
1. let
- Block Scope: Variables declared with
letare block-scoped, only accessible within the block they are defined in (e.g., within{}braces). - Re-declaration: cannot be re-declared within the same scope.
- hoisted to the top of their block, but they are not initialized.
- Accessing before declaration results in a
ReferenceError.
Example:
let x = 10;
if (true) {
let x = 20; // different `x` than the one outside
console.log(x); // 20
}
console.log(x); // 102. var
- Function Scope: Variables declared with
varare function-scoped, - Re-declaration: Variables declared with
varcan be re-declared within the same scope.
Example:
var y = 10;
if (true) {
var y = 20; // same `y` as the one outside
console.log(y); // 20
}
console.log(y); // 203. const
- Block Scope: Variables declared with
constare block-scoped, likelet. - Re-declaration: Variables declared with
constcannot be re-declared within the same scope. - Immutability: The value of a
constvariable cannot be changed through reassignment. However, if theconstvariable is an object or array, its properties or elements can be modified. - Hoisting: Variables declared with
constare hoisted to the top of their block, but they are not initialized. Accessing them before the declaration results in aReferenceError.
Example:
const z = 10;
// z = 20; // Error: Assignment to constant variable
const obj = { a: 1 };
obj.a = 2; // This is allowed
console.log(obj.a); // 2Data types
1. Primitive Data Types
These are immutable and stored by value.
- Number
- Represents both integers and floating-point numbers.
- Example:
42,3.14,Infinity,NaN.
- String
- Represents a sequence of characters.
- Example:
'Hello',"World",`Template String`.
- Boolean
- Represents logical values.
- Example:
true,false.
- Undefined
- A variable that has been declared but not initialized.
- Example:
let x; // x is undefined.
- Null
- Represents an intentional absence of value.
- Example:
let y = null;.
- Symbol (ES6)
- Represents unique, immutable identifiers.
- Example:
const sym = Symbol('description');.
- BigInt
- Used for integers larger than the
Numbertype can safely handle. - Example: `const bigNum = 123456789012345678901234567890n;
- Used for integers larger than the
2. Non-Primitive (Reference) Data Types
These are mutable and stored by reference.
- Object
- Represents collections of key-value pairs.
- Example:
const obj = { name: 'John', age: 25 };.
- Array*
- Represents an ordered collection of values.
- Example:
const arr = [1, 2, 3];.
- Function
- Represents callable blocks of code.
- Example:
function greet() { console.log('Hello'); }.
- Date
- Represents date and time.
Type Conversion in JavaScript
Implicit Type Conversion (Type Coercion)
Occurs automatically during operations.
- String Concatenation:
5 + '5'→'55'(Number → String)
- Arithmetic Operations:
'5' - 2→3(String → Number)
- Boolean Coercion:
if ('')→false(Empty string → Boolean)
2. Explicit Type Conversion
Performed manually using methods.
-
To String
String(value)- Example:
String(123)→'123'.
-
To Number
Number(value)- Example:
Number('123')→123. - Using
parseIntorparseFloat:parseInt('42.5')→42.
-
To Boolean*
Boolean(value)- Example:
Boolean(0)→false.
-
Falsey Values:
false,0,'',null,undefined,NaN.
Variable hoisting
- Hoisting moves variable and function declarations to the top of their scope before execution.
- var is hoisted with an initial value of undefinedv
- let and const are hoisted but remain in the Temporal Dead Zone until assigned.