π JavaScript DSA Series - Fundamentals
π¦ Variables & Constants
-
Variable: A variable is like a container that stores a value. This value can change (or vary) as the program runs.
let name = "Alice";
-
Constant: A constant is similar to a variable, but once assigned a value, it cannot be changed.
const PI = 3.14;
-
const
Keyword: Declares a constant whose value can't be reassigned.
π³ Data Types in JavaScript
JavaScript variables can hold different types of data. These types are categorized into Primitive and Non-Primitive (Reference types).
π§© Data Types Tree Structure
Data Types
βββ Primitive
β βββ Number
β βββ String
β βββ Boolean
β βββ Null
β βββ Undefined
β βββ Symbol
β βββ BigInt
βββ Non-Primitive (Reference)
βββ Linear
β βββ Array
β βββ Linked List (user-defined)
βββ Non-Linear
βββ Tree (user-defined)
βββ Graph (user-defined)
πΉ Primitive Data Types
Type | Description | Example |
---|---|---|
Number | Represents integers and floating-point numbers | let age = 25; |
String | Represents textual data | let name = "Alice"; |
Boolean | Represents true or false | let isLoggedIn = true; |
Null | Represents intentional empty value | let data = null; |
Undefined | Declared variable but not assigned value | let x; console.log(x); |
Symbol | Unique identifier (mostly for object keys) | const id = Symbol("id"); |
BigInt | Large integers beyond safe number range | let big = 123456789012345678901234567890n; |
πΈ Non-Primitive (Reference) Data Types
πΉ Linear Data Structures
1. Array
- Stores elements in contiguous memory locations.
- Elements can be accessed via index.
- Best for random access.
- Dynamic in size in JS.
let arr = [10, 20, 30];
console.log(arr[1]); // 20
2. Linked List
- Consists of nodes, where each node contains data and a reference (pointer) to the next node.
- Does not store data in contiguous memory.
- Useful for dynamic insertions and deletions.
// Example representation
let node = {
data: 10,
next: {
data: 20,
next: null
}
};
πΉ Non-Linear Data Structures
1. Tree
- A hierarchical structure where each node can have multiple children.
- Common types: Binary Tree, Binary Search Tree (BST).
- Useful in representing the DOM, folder structures, etc.
2. Graph
- A set of nodes (vertices) connected by edges.
- Can be directed/undirected, cyclic/acyclic.
- Used in routing algorithms, social networks, etc.
β οΈ Note: Tree and Graph are implemented manually in JS using objects/classes.
Operators vs Operands in JavaScript
In JavaScript, operators and operands are fundamental building blocks for performing operations. Understanding them is crucial for writing effective code.
What is an Operator?
An operator is a symbol that performs an operation on one or more operands.
let result = 10 + 5;
// Here, '+' is the operator (performs addition)
What is an Operand?
An operand is the value or variable on which the operator acts.
let result = 10 + 5;
// Here, '10' and '5' are operands
π Types of Operators
1οΈβ£ By Number of Operands
πΈ Unary Operators
- Work on a single operand
- Examples:
-x
,++x
,!x
let x = 5;
let y = -x; // Unary minus
let z = !x; // Logical NOT
πΈ Binary Operators
- Work on two operands
- Most common type
let sum = a + b; // Addition operator
let product = x * y; // Multiplication operator
πΈ Ternary Operator
- Works on three operands
- Used as a shorthand for if-else
let result = (age >= 18) ? "Adult" : "Minor";
2οΈβ£ By Operation Type
πΈ Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 15 / 3 = 5 |
% | Modulus (Remainder) | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
πΈ Comparison Operators
Operator | Description | Example |
---|---|---|
== | Equal (loose) | 5 == "5" β true |
=== | Equal (strict) | 5 === "5" β false |
!= | Not equal (loose) | 5 != "6" β true |
!== | Not equal (strict) | 5 !== "5" β true |
> | Greater than | 5 > 3 β true |
< | Less than | 5 < 3 β false |
>= | Greater than or equal | 5 >= 5 β true |
<= | Less than or equal | 5 <= 3 β false |
πΈ Logical Operators
Operator | Description | Example |
---|---|---|
&& | Logical AND | true && false β false |
|| | Logical OR | true || false β true |
! | Logical NOT | !true β false |
πΈ Assignment Operators
Operator | Description | Example |
---|---|---|
= | Simple assignment | x = 5 |
+= | Add and assign | x += 3 β x = x + 3 |
-= | Subtract and assign | x -= 3 β x = x - 3 |
*= | Multiply and assign | x *= 3 β x = x * 3 |
/= | Divide and assign | x /= 3 β x = x / 3 |
%= | Modulus and assign | x %= 3 β x = x % 3 |
πΈ Increment & Decrement
Operator | Description | Example |
---|---|---|
++ | Increment by 1 | x++ or ++x |
-- | Decrement by 1 | x-- or --x |
// Prefix vs Postfix
let x = 5;
let y = x++; // y = 5, x = 6
let z = ++x; // x = 7, z = 7
πΈ Bitwise Operators
Operator | Description | Example |
---|---|---|
& | Bitwise AND | 5 & 3 β 1 |
| | Bitwise OR | 5 | 3 β 7 |
^ | Bitwise XOR | 5 ^ 3 β 6 |
~ | Bitwise NOT | ~5 β -6 |
<< | Left shift | 5 << 1 β 10 |
>> | Right shift | 5 >> 1 β 2 |
π Best Practices
-
Operator Precedence
- Use parentheses to make operations clear
- Don't rely on default operator precedence
let result = (a + b) * c; // Clear intention
-
Type Coercion
- Use strict equality (
===
) over loose equality (==
) - Be explicit about type conversions
let num = Number("123"); // Explicit conversion
- Use strict equality (
-
Short-Circuit Evaluation
- Use
&&
and||
for conditional execution
const name = user && user.name; // Safe property access const defaultName = userName || "Guest"; // Default value
- Use
π Examples
Practical Usage
// Combining multiple operators
let score = 85;
let passing = 60;
let excellent = 90;
let result = score >= passing ? (score >= excellent ? "Excellent!" : "Pass") : "Fail";
// Short-circuit evaluation
function getUser(id) {
const user = findUser(id);
return user && user.isActive && user.name;
}
// Chaining assignments
let a, b, c;
a = b = c = 5; // All variables get value 5
β οΈ Common Pitfalls
-
Unintended Type Coercion
// Avoid if (5 == "5") { ... } // true // Prefer if (5 === "5") { ... } // false
-
Post/Pre Increment/Decrement Confusion
let x = 5; console.log(x++); // Shows 5, then x becomes 6 console.log(++x); // x becomes 7, then shows 7
-
Operator Precedence Issues
// Unclear let result = a + b * c; // Clear let result = a + (b * c);
π€ ASCII Table (Common Characters)
Character | ASCII Code | Description |
---|---|---|
A-Z | 65-90 | Uppercase letters |
a-z | 97-122 | Lowercase letters |
0-9 | 48-57 | Digits |
Space | 32 | Space character |
! | 33 | Exclamation mark |
" | 34 | Double quote |
# | 35 | Hash |
$ | 36 | Dollar sign |
% | 37 | Percent |
& | 38 | Ampersand |