logo

JavaScript Fundamentals

Learn the core concepts of JavaScript programming

JavaScript Fundamentals

JavaScript is the programming language of the web. This guide covers the essential concepts you need to know.

Variables and Data Types

// Variables
let name = "John";
const age = 25;
var isStudent = true;

// Data types
let number = 42;
let string = "Hello World";
let boolean = true;
let array = [1, 2, 3];
let object = { name: "John", age: 25 };

Functions

// Function declaration
function greet(name) {
    return `Hello, ${name}!`;
}

// Arrow function
const add = (a, b) => a + b;

// Function expression
const multiply = function(a, b) {
    return a * b;
};

Control Flow

// If statement
if (age >= 18) {
    console.log("Adult");
} else {
    console.log("Minor");
}

// For loop
for (let i = 0; i < 5; i++) {
    console.log(i);
}

// While loop
let count = 0;
while (count < 3) {
    console.log(count);
    count++;
}

Objects and Arrays

// Object
const person = {
    name: "Alice",
    age: 30,
    greet() {
        return `Hi, I'm ${this.name}`;
    }
};

// Array methods
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
const evens = numbers.filter(n => n % 2 === 0);
const sum = numbers.reduce((acc, n) => acc + n, 0);

Master these fundamentals and you'll have a solid foundation for JavaScript development!