JavaScript conditional statements allow you to perform different actions based on different conditions. Here are the most common types:
if statement
else statement
else if statement
Activities
Activity 01
Write a JavaScript code snippet to display a message based on the current studentsâ marks,If the marks is above 40, display âPass the Exam!â.Assume that the current student mark is 60.
Activity 02
Youâre developing a weather application. Write a JavaScript code snippet to display a message based on the current temperature.
If the temperature is above 20 degrees Celsius, display âItâs a warm day!â. Otherwise, display âItâs a cool day!â. Take the current temperature from the user as an input.
The output should be as below.
Activity 03
Write a JavaScript code snippet to display a message based on the current studentsâ scores:
If the score is above or equals to 90, display âExcellentâ.Else If the score is above or equals to 80, display âGoodâ. Else If the score is above or equals to 70, display âAverageâ. Else If the score is above or equals to 60, display âBelow Averageâ. Otherwise print âFailâ.
Assume that the current score is 75.
Activity 04
Youâre designing a system to determine shipping costs for an eCommerce website.Write a JavaScript code snippet to user can enter the weight of the package in kilograms for
shipment. Display the shipping cost according to the weight entered by the user.
âȘIf the weight is less than or equal to 5 kg, the shipping cost is 10.âȘIftheweightisbetween5kg(exclusive)and10kg(inclusive),theshippingcostis15.
âȘIf the weight is greater than 10 kg, the shipping cost is $20.
Activity 05
Youâre developing a real estate application for listing land properties.Write a JavaScript code snippet to prompt the user to enter the area of the land property (in square meters). After the user enters the area, calculate the price of the land property according to the area entered:
âȘIf the area is less than or equal to 100 square meters, display an alert box with the price of 10,000.âȘIftheareaisbetween101squaremeters(exclusive)and200squaremeters(inclusive),displayanalertboxwiththepriceof15,000.
âȘIf the area is greater than 200 square meters, display an alert box with the price of $20,000.
JavaScript Loops
For Loop : When you know in advance how many times you want to execute a block of code.
While Loop : When you want to repeat code until a certain condition is no longer true, and you donât necessarily know in advance how many iterations will be needed.
Do-While Loop
 Similar to the while loop, but it guarantees that the code block will run at least once, because the condition is checked after the loop executes.
Arrays in JavaScript
 Arrays in JavaScript are a way to store multiple values in a single variable. Theyâre like lists where you can keep any type of data, from numbers and strings to objects and other arrays.
NOTE
Arrays in JavaScript ainât much focused on our module as i know, Just gotta have an idea of what an Array is.
JavaScript Functions
A function is a block of code designed to do a particular task. You can think of it as a reusable piece of code that you can call whenever you need to perform that task.
How to create a function?
âą functionName: The name of the function (e.g., greet).
âą parameters: Values you pass into the function (e.g., name).
âą code to be executed: What the function does (e.g., console.log(âHello, â + name + â!â)).
How to call a function that previously declared?
you can call it by using its name and providing the necessary arguments.
Advantages of functions?
Code reusability: We can call a function several times, so it save coding.
Less coding: It makes our program compact. We donât need to write many lines of code each