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):
- A variable named `userName` with the value "Ahmed" (mutable).
- A variable named `PI` with the value 3.14 (immutable).
- 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:
- A string named `message` containing "Hello".
- A number named `age` with the value 25.
- A boolean named `isStudent` with the value true.
- 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:
- Define two variables `x = 10` and `y = 5`.
- Print their sum.
- Print their product.
- 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:
- You have the variables: `firstName = "Ali"`, `lastName = "Mohammed"`.
- Create a `fullName` variable that combines both names with a space.
- 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`:
- It takes one parameter called `name`.
- It returns the string "Hello, [name]!".
- 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:
- Is `5 == "5"` (value equality)?
- Is `5 === "5"` (value and type equality)?
- 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:
- Include the properties: `brand` (Toyota), `model` (Corolla), `year` (2020).
- Change `year` to 2022.
- 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:
- Create an array `colors` with: "Red", "Green", "Blue".
- Print the first element of the array.
- Add a new color "Yellow" to the end of the array (push).
- 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:
- Define `score` with the value 10.
- Add 5 to `score` using `+=`.
- Multiply `score` by 2 using `*=`.
- 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:
- Write a single-line comment.
- Write a multi-line comment.
- Use `console.error()` to print a fake error message "Something went wrong".
- 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");