- Variables
Declare and manage data.
- Data Types
Understanding primitive and object types.
- Operators
Perform operations on values.
- Control Flow
Conditional statements and loops.
- Functions
Reusable blocks of code.
- Objects
Key-value pairs and properties.
- Arrays
Ordered collections of data.
JavaScript Language Reference
Core Concepts
Advanced Topics
- Prototypes and Inheritance
Object-oriented programming concepts.
- Closures
Accessing outer function scope from inner functions.
- Asynchronous JavaScript
Callbacks, Promises, and Async/Await.
- Modules
Organizing code into reusable units.
- Error Handling
Managing exceptions and errors.
Built-in Objects
- String Object
Manipulating text.
- Number Object
Mathematical operations.
- Math Object
Constants and mathematical functions.
- Date Object
Working with dates and times.
- RegExp Object
Regular expressions for pattern matching.
- JSON Object
Parsing and stringifying JSON data.
Key Features
Variables
Variables are containers for storing data values. In modern JavaScript, `let` and `const` are preferred over `var`.
// Declaring variables
let message = "Hello, World!";
const PI = 3.14159;
// Reassigning a 'let' variable
message = "Greetings!";
// Attempting to reassign a 'const' variable will cause an error
// PI = 3.14; // TypeError
Data Types
JavaScript has primitive types (String, Number, Boolean, Null, Undefined, Symbol, BigInt) and the Object type.
let name = "Alice"; // String
let age = 30; // Number
let isStudent = false; // Boolean
let job = null; // Null (intentional absence of a value)
let apartment; // Undefined (variable declared but not assigned)
const id = Symbol('unique'); // Symbol
const bigNum = 9007199254740991n; // BigInt
let person = { name: "Bob", age: 25 }; // Object
let numbers = [1, 2, 3]; // Array (a type of object)
Operators
Operators perform operations on operands. Common operators include arithmetic, assignment, comparison, and logical.
// Arithmetic
let sum = 10 + 5; // 15
let diff = 10 - 5; // 5
let product = 10 * 5; // 50
let quotient = 10 / 5; // 2
let remainder = 10 % 3; // 1
// Comparison
let isEqual = (10 == '10'); // true (type coercion)
let isStrictEqual = (10 === '10'); // false (no type coercion)
let isGreater = (10 > 5); // true
// Logical
let isTrue = true;
let isFalse = false;
let logicalAnd = isTrue && isFalse; // false
let logicalOr = isTrue || isFalse; // true
let logicalNot = !isTrue; // false
Control Flow
Control flow statements dictate the order in which code is executed.
// if-else statement
let score = 75;
if (score >= 90) {
console.log("Excellent!");
} else if (score >= 70) {
console.log("Good job!");
} else {
console.log("Keep practicing.");
}
// for loop
for (let i = 0; i < 5; i++) {
console.log(`Iteration ${i}`);
}
// while loop
let count = 0;
while (count < 3) {
console.log(`While count: ${count}`);
count++;
}
Functions
Functions are blocks of reusable code that perform a specific task.
// Function declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Function expression
const add = function(a, b) {
return a + b;
};
// Arrow function (concise syntax)
const multiply = (x, y) => x * y;
console.log(greet("Alice")); // Output: Hello, Alice!
console.log(add(5, 3)); // Output: 8
console.log(multiply(4, 6)); // Output: 24
Objects
Objects are collections of properties, where each property is a key-value pair.
let car = {
make: "Toyota",
model: "Camry",
year: 2022,
isRunning: false,
start: function() {
this.isRunning = true;
console.log("Engine started.");
}
};
console.log(car.make); // Output: Toyota
console.log(car['model']); // Output: Camry
car.start(); // Output: Engine started.
console.log(car.isRunning); // Output: true
Arrays
Arrays are ordered lists of values, accessed by index.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple
console.log(fruits.length); // Output: 3
fruits.push("Date"); // Add to end
console.log(fruits); // ["Apple", "Banana", "Cherry", "Date"]
fruits.pop(); // Remove from end
console.log(fruits); // ["Apple", "Banana", "Cherry"]
fruits.forEach(fruit => {
console.log(`I like ${fruit}`);
});