Basic Practice Problems Of Js

Posted on January 22, 2025 by Vishesh Namdev
Python C C++ Javascript Java
Top most asked Basic Practice Problem of JS

Learn JavaScript Fundamentals: Variables, Operators, and More

Learn the basics of JavaScript programming, including variables, operators, control structures, functions, and more.

1. Question: Write a simple program in JavaScript to calculate the area of a rectangle.

  • Take inputs for length and width, and output the result.
  • // Program to calculate area of a rectangle
    const length = 10; // Length of the rectangle
    const width = 5;   // Width of the rectangle
      
    const area = length * width; // Formula for area
    console.log("The area of the rectangle is:", area);

    2. Question: Write a program that takes a user's name as input, stores it in a variable, and prints a greeting message. Add comments to explain each step.

    // Declare a variable to store the user's name
    const userName = "Alice"; // Example name
      
    // Print a greeting message
    console.log("Hello, " + userName + "! Welcome to JavaScript.");

    3. Question: Write a program that demonstrates the difference between const, let, and var. Show how re-assignment works and where it doesn't.

      // Using const - value cannot be reassigned
      const pi = 3.14;
      // pi = 3.1415; // This will throw an error
      
      // Using let - value can be reassigned
      let age = 25;
      age = 26; // Re-assignment works
      
      // Using var - value can also be reassigned
      var color = "red";
      color = "blue"; // Re-assignment works
      
      console.log("Const example:", pi);
      console.log("Let example:", age);
      console.log("Var example:", color);

    4. Question: Write a program to demonstrate the difference between a primitive type and an object in JavaScript.

      // Primitive type example
      let primitiveValue = 10;
      let anotherValue = primitiveValue; // Copy the value
      anotherValue = 20; // Change the copied value
      console.log("Primitive Value:", primitiveValue); // Output: 10
      
      // Object example
      let objectValue = { name: "Alice" };
      let anotherObject = objectValue; // Reference the same object
      anotherObject.name = "Bob"; // Modify the object
      console.log("Object Value:", objectValue); // Output: { name: "Bob" }
      

    5. Question: Write a program to take two numbers as input and use different operators to calculate and print the results for: Addition, Subtraction, Multiplication, Division and Modulus.

      // Inputs
      const num1 = 20;
      const num2 = 5;
      
      // Performing operations
      console.log("Addition:", num1 + num2);         // Output: 25
      console.log("Subtraction:", num1 - num2);      // Output: 15
      console.log("Multiplication:", num1 * num2);   // Output: 100
      console.log("Division:", num1 / num2);         // Output: 4
      console.log("Modulus:", num1 % num2);          // Output: 0
      
    📢Important Note📢