JavaScript Fundamentals

Introduction to JavaScript

JavaScript is a powerful, versatile programming language that is the cornerstone of modern web development. It enables dynamic and interactive content on websites, making them engaging for users. Unlike HTML (which structures content) and CSS (which styles content), JavaScript adds behavior.

This tutorial will cover the essential building blocks of JavaScript that every developer should know.

Variables and Data Types

Variables are containers for storing data values. In JavaScript, you can declare variables using var, let, and const.

Common Data Types:


let greeting = "Hello";
const pi = 3.14159;
let isLearning = true;
let numbers = [10, 20, 30];
let person = {
    firstName: "John",
    lastName: "Doe"
};
            

Operators

Operators are symbols that perform operations on values (operands).


let x = 10;
let y = 5;
let sum = x + y; // 15
let isEqual = (x === 10); // true
let isGreater = (x > y); // true
let condition = (x > 0 && y < 10); // true
            

Control Flow Statements

Control flow statements allow you to control the execution of your code based on certain conditions.

Conditional Statements:


let hour = 14;
if (hour < 12) {
    console.log("Good morning!");
} else if (hour < 18) {
    console.log("Good afternoon!");
} else {
    console.log("Good evening!");
}

let day = 3;
switch (day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    default:
        console.log("Another day");
}
            

Loops:


for (let i = 0; i < 5; i++) {
    console.log("Count: " + i);
}

let j = 0;
while (j < 3) {
    console.log("While loop: " + j);
    j++;
}

const colors = ["red", "green", "blue"];
for (const color of colors) {
    console.log(color);
}
            

Functions

Functions are blocks of reusable code designed to perform a particular task. They help in organizing code and reducing redundancy.


// Function declaration
function greet(name) {
    return "Hello, " + name + "!";
}

// Function expression
const add = function(a, b) {
    return a + b;
};

// Arrow function (ES6+)
const multiply = (a, b) => a * b;

console.log(greet("Alice"));      // Output: Hello, Alice!
console.log(add(5, 3));           // Output: 8
console.log(multiply(4, 6));      // Output: 24
            

Objects and Arrays

These are fundamental data structures in JavaScript.

Objects:

Objects are collections of properties, where each property has a key (name) and a value.


const car = {
    make: "Toyota",
    model: "Camry",
    year: 2022,
    isElectric: false,
    start: function() {
        console.log("Engine started!");
    }
};

console.log(car.make);       // Output: Toyota
console.log(car["model"]);   // Output: Camry
car.start();                 // Output: Engine started!
            

Arrays:

Arrays are ordered lists that can hold various data types.


const fruits = ["Apple", "Banana", "Orange"];
console.log(fruits.length);      // Output: 3
fruits.push("Mango");            // Add to the end
console.log(fruits);             // ["Apple", "Banana", "Orange", "Mango"]
fruits.shift();                  // Remove from the beginning
console.log(fruits);             // ["Banana", "Orange", "Mango"]

fruits.forEach(function(fruit) {
    console.log("I like " + fruit);
});
            
Tip: Use const by default for variables unless you know you'll need to reassign them. This promotes immutability and makes your code easier to reason about.
Note: Understanding these fundamental concepts is crucial for building any interactive web application. Practice these concepts frequently to solidify your understanding.