Basic Arithmetic Operations
Image: Output of Basic Arithmetic Operations into the console
Objective:
To familiarize the student with basic arithmetic operations in JavaScript and the usage of variables.
Topics Covered:
- Variables
- Arithmetic Operations
Code:
// Objective: Perform arithmetic operations and store their results in variables.
// 1. Declare two variables, a and b, and assign them values of 5 and 3, respectively.
let a = 5;
let b = 3;
// 2. Declare a variable named sum and assign it the result of adding a and b.
let sum = a + b;
// 3. Declare a variable named difference and assign it the result of subtracting b from a.
let difference = a – b;
// 4. Declare a variable named product and assign it the result of multiplying a and b.
let product = a * b;
// 5. Declare a variable named quotient and assign it the result of dividing a by b.
let quotient = a / b;
// 6. Print the results to the console.
console.log(“Sum: ” + sum);
console.log(“Difference: ” + difference);
console.log(“Product: ” + product);
console.log(“Quotient: ” + quotient);
// Exercise: Modify values of a and b and observe the changes in the output.
Explanation:
This exercise introduces the basic arithmetic operations: addition, subtraction, multiplication, and division. Students will learn how to declare variables and use them to store the results of arithmetic operations, as well as how to print these results to the console.