Skip to content

Javascript

Hello World Program

console.log("Hello, world!");

Variables and Data Types

// Variable declaration and assignment
let x = 10;
const pi = 3.14159;
let name = "John";

// Data types
let i = 10;
let d = 3.14159;
let s = "Hello";
let b = true;
let a = [1, 2, 3];
let o = {one: 1, two: 2};

Operators

let x = 10;
let y = 20;

// Arithmetic operators
let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y;
let remainder = x % y;

// Comparison operators
let isEqual = x == y;
let isNotEqual = x != y;
let isGreaterThan = x > y;
let isLessThan = x < y;
let isGreaterThanOrEqual = x >= y;
let isLessThanOrEqual = x <= y;

// Logical operators
let and = x > 0 && y > 0;
let or = x > 0 || y > 0;
let not = !(x > 0);

Conditional Statements

let x = 10;

// If statement
if (x > 0) {
    console.log("x is positive");
}

// If-else statement
if (x > 0) {
    console.log("x is positive");
} else {
    console.log("x is negative");
}

// Else-if statement
if (x > 0) {
    console.log("x is positive");
} else if (x == 0) {
    console.log("x is zero");
} else {
    console.log("x is negative");
}

Loops

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

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

// Do-while loop
let i = 0;
do {
    console.log(i);
    i++;
} while (i < 10);

// For-in loop
let obj = {one: 1, two: 2, three: 3};
for (let key in obj) {
    console.log(key + ": " + obj[key]);
}

// For-of loop
let arr = [1, 2, 3];
for (let elem of arr) {
    console.log(elem);
}

Functions

// Function declaration
function add(x, y) {
    return x + y;
}

// Function expression
let add = function(x, y) {
    return x + y;
};

// Arrow function
let add = (x, y) => {
    return x + y;
};

// Default parameters
function greet(name="John") {
    console.log("Hello, " + name + "!");
}

// Rest parameters
function sum(...nums) {
    let total = 0;
    for (let num of nums) {
        total += num;
    }
    return total;
}

// Higher-order function
function double(x) {
    return x * 2;
}
function apply(func, x) {
    return func(x);
}
let result = apply(double, 10);
console.log(result);

Arrays

// Array declaration and initialization
let a = [1, 2, 3, 4, 5];

// Accessing elements of an array
let firstElement = a[0];
let lastElement = a[a.length - 1];

// Adding elements to an array
a.push(6);
a.unshift(0);

// Removing elements from an array
a.pop();
a.shift();

Objects

// Object declaration and initialization
let person = {
    name: "John",
    age: 30,
    gender: "male"
};

// Accessing properties of an object
let name = person.name;
let age = person["age"];

// Adding properties to an object
person.email = "john@example.com";

// Removing properties from an object
delete person.gender;

Functions

// Function declaration
function add(x, y) {
    return x + y;
}

// Function expression
let add = function(x, y) {
    return x + y;
};

// Arrow function
let add = (x, y) => x + y;

Classes

// Class declaration
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    getName() {
        return this.name;
    }
    getAge()