JavaScript: Add Two Numbers

1. Introduction

In the journey of learning JavaScript, a common exercise that beginners encounter is adding two numbers. Though it might seem basic, this task is essential as it allows learners to grasp the fundamental concepts of data input, processing, and output in JavaScript.

2. Program Overview

This simple JavaScript program will:

1. Declare two variables to hold our numbers.

2. Add the numbers together.

3. Display the result.

3. Code Program

let num1 = 5;  // First number to be added
let num2 = 7;  // Second number to be added
let sum = num1 + num2;  // Resultant sum of num1 and num2
console.log("The sum of " + num1 + " and " + num2 + " is: " + sum);

Output:

The sum of 5 and 7 is: 12

4. Step By Step Explanation

1. Variable Declaration: We begin by declaring two variables, num1, and num2, initialized with the values 5 and 7 respectively. These will hold the numbers we want to add.

2. Addition: The statement let sum = num1 + num2; adds the values stored in num1 and num2 and assigns the result to the variable sum.

3. Displaying the Result: The console.log function then displays the resultant sum. The output is a concatenated string that provides context to the result.

Comments