1. Hello World Program
 

// Program to display "Hello, World!" in the console
console.log("Hello, World!");

Explanation:

This program uses console.log() to output the string "Hello, World!" to the console.
console.log() is a method in JavaScript used for logging messages or data to the browser's console.



2. Variables and Data Types
 
// Program to demonstrate variables and data types
let name = "John"; // String variable
let age = 30; // Number variable
let isStudent = true; // Boolean variable

console.log("Name:", name);
console.log("Age:", age);
console.log("Is Student:", isStudent);


Explanation:
  • This program declares variables of different data types: string (name), number (age), and boolean (isStudent).
  • It assigns values to these variables and then logs their values to the console using console.log().


3. Arithmetic Operations
 

// Program to perform arithmetic operations
let num1 = 10;
let num2 = 5;

let sum = num1 + num2;
let difference = num1 - num2;
let product = num1 * num2;
let quotient = num1 / num2;

console.log("Sum:", sum);
console.log("Difference:", difference);
console.log("Product:", product);
console.log("Quotient:", quotient);
 


Explanation:

 
This program performs basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers (num1 and num2).
It calculates the sum, difference, product, and quotient of these numbers and logs the results to the console.


4. Conditional Statements (If-Else)

 

// Program to demonstrate if-else statement
let marks = 85;

if (marks >= 90) {
    console.log("Grade: A+");
} else if (marks >= 80) {
    console.log("Grade: A");
} else if (marks >= 70) {
    console.log("Grade: B");
} else if (marks >= 60) {
    console.log("Grade: C");
} else {
    console.log("Grade: Fail");
}


Explanation:
  • This program uses an if-else statement to determine the grade based on the value of marks.
  • Depending on the value of marks, it outputs the corresponding grade using console.log().

5. Looping (For Loop)
 

// Program to display numbers from 1 to 5 using a for loop
for (let i = 1; i <= 5; i++) {
    console.log(i);
}


Explanation:
 
  • This program uses a for loop to display numbers from 1 to 5.
  • The loop initializes a variable i to 1, increments i by 1 in each iteration, and continues as long as i is less than or equal to 5.
  • It logs the value of i in each iteration, resulting in numbers 1, 2, 3, 4, and 5 being displayed.

6. Functions

 

// Program to define and call a function
function greet(name) {
    return "Hello, " + name + "!";
}

let message = greet("Alice");
console.log(message);


Explanation:
  • This program defines a function greet that takes a parameter name and returns a greeting message.
  • It calls the greet function with the argument "Alice" and stores the returned message in the variable message.
  • Finally, it logs the message to the console.

7. Arrays
 

// Program to demonstrate arrays
let fruits = ["Apple", "Banana", "Orange"];

console.log("First Fruit:", fruits[0]);
console.log("Total Fruits:", fruits.length);

fruits.push("Mango");
console.log("Updated Fruits:", fruits);
 


Explanation:
  • This program creates an array fruits containing three elements: "Apple", "Banana", and "Orange".
  • It accesses the first element of the array using fruits[0] and logs its value.
  • It also logs the total number of elements in the array using fruits.length.
  • The program then adds a new element ("Mango") to the array using the push() method and logs the updated array.


8. Objects
 
// Program to demonstrate objects
let person = {
    name: "John",
    age: 30,
    isStudent: true
};

console.log("Name:", person.name);
console.log("Age:", person["age"]);
console.log("Is Student:", person.isStudent);


Explanation:
  • This program defines an object person with properties such as name, age, and isStudent.
  • It accesses these properties using dot notation (person.name and person.isStudent) as well as bracket notation (person["age"]) and logs their values.
9. String Manipulation
 
// Program to manipulate strings
let message = "Hello, World!";

console.log("Length:", message.length);
console.log("Uppercase:", message.toUpperCase());
console.log("Substring:", message.substring(7, 12));
console.log("Split:", message.split(","));


Explanation:
  • This program manipulates a string variable message.
  • It calculates the length of the string using message.length.
  • It converts the string to uppercase using message.toUpperCase().
  • It extracts a substring from the string using message.substring(7, 12) (from index 7 to 11).
  • It splits the string into an array using message.split(",") based on the comma delimiter.

10. Error Handling (Try-Catch)

 
// Program to demonstrate try-catch for error handling
try {
    let x = 10 / 0; // Division by zero (causes an error)
    console.log("Result:", x);
} catch (error) {
    console.log("Error:", error.message);
}


Explanation:
 
  • This program demonstrates error handling using a try-catch block.
  • Inside the try block, it attempts to perform a division by zero operation (10 / 0), which causes an error.
  • The catch block catches the error and logs the error message using error.message.
  • These JavaScript programs cover a range of fundamental concepts such as variables, data types, conditional statements, loops, functions, arrays, objects, string manipulation, and error handling. Each program includes code snippets with explanations to help you understand how to write and execute JavaScript code.



Practice Excercise Practice now