JS Exam 1: Variables, Types, and Functions (10 mixed exercises)

JS Exam 1: JavaScript Fundamentals

Test your JavaScript basics, from defining variables to writing simple functions.

Exercise 1 Variables

Define the following variables using the appropriate keywords (let, const):

  1. A variable named `userName` with the value "Ahmed" (mutable).
  2. A variable named `PI` with the value 3.14 (immutable).
  3. Try changing `PI` to 3.15 and explain what happens in a comment.
Solution
let userName = "Ahmed";
const PI = 3.14;

// PI = 3.15;
// This will throw a TypeError because const variables cannot be reassigned.
Exercise 2 Data Types

Create variables that represent the following types:

  1. A string named `message` containing "Hello".
  2. A number named `age` with the value 25.
  3. A boolean named `isStudent` with the value true.
  4. Check the type of `age` using `typeof` and log the result to the console.
Solution
let message = "Hello";
let age = 25;
let isStudent = true;

console.log(typeof age); // "number"
Exercise 3 Arithmetic Operators

Perform the following operations:

  1. Define two variables `x = 10` and `y = 5`.
  2. Print their sum.
  3. Print their product.
  4. Print the remainder of `x` divided by `3` (modulus).
Solution
let x = 10;
let y = 5;

console.log(x + y); // 15
console.log(x * y); // 50
console.log(x % 3); // 1 (10 / 3 = 3 remainder 1)
Exercise 4 String Concatenation

Build a full sentence:

  1. You have the variables: `firstName = "Ali"`, `lastName = "Mohammed"`.
  2. Create a `fullName` variable that combines both names with a space.
  3. Use template literals (`` ` ``) to print "Hello, [full name]".
Solution
let firstName = "Ali";
let lastName = "Mohammed";

let fullName = firstName + " " + lastName;

console.log(`Hello, ${fullName}`);
Exercise 5 Basic Functions

Create a function named `greet`:

  1. It takes one parameter called `name`.
  2. It returns the string "Hello, [name]!".
  3. Call the function with the name "Sarah" and log the result.
Solution
function greet(name) {
    return "Hello, " + name + "!";
}

console.log(greet("Sarah")); // Hello, Sarah!
Exercise 6 Comparison Operators

What are the results (true/false)? Write code to verify:

  1. Is `5 == "5"` (value equality)?
  2. Is `5 === "5"` (value and type equality)?
  3. Is `10 > 5` and `5 < 2` (use AND `&&`)?
Solution
console.log(5 == "5");   // true
console.log(5 === "5");  // false
console.log(10 > 5 && 5 < 2); // false (second condition is false)
Exercise 7 Basic Objects

Create an object that represents a car:

  1. Include the properties: `brand` (Toyota), `model` (Corolla), `year` (2020).
  2. Change `year` to 2022.
  3. Print "My car is [brand] [model]" using the object properties.
Solution
let car = {
    brand: "Toyota",
    model: "Corolla",
    year: 2020
};

car.year = 2022;

console.log(`My car is ${car.brand} ${car.model}`);
Exercise 8 Arrays

Work with a list of colors:

  1. Create an array `colors` with: "Red", "Green", "Blue".
  2. Print the first element of the array.
  3. Add a new color "Yellow" to the end of the array (push).
  4. Print the array length.
Solution
let colors = ["Red", "Green", "Blue"];

console.log(colors[0]); // Red

colors.push("Yellow");

console.log(colors.length); // 4
Exercise 9 Assignment Operators

Use shorthand operators to update a variable:

  1. Define `score` with the value 10.
  2. Add 5 to `score` using `+=`.
  3. Multiply `score` by 2 using `*=`.
  4. Decrement `score` by 1 using `--`.
Solution
let score = 10;

score += 5; // now 15
score *= 2; // now 30
score--;    // now 29

console.log(score);
Exercise 10 Comments & Console

Do the following:

  1. Write a single-line comment.
  2. Write a multi-line comment.
  3. Use `console.error()` to print a fake error message "Something went wrong".
  4. Use `console.warn()` to print a warning "Be careful".
Solution
// This is a single-line comment

/*
  This is a
  multi-line comment
*/

console.error("Something went wrong");
console.warn("Be careful");
Smart Editor

Write code and see the result instantly

Try it free