How to make a javaScript addition

Learn how to add two numbers together using JavaScript, with an example for beginners.

Adding Numbers with JavaScript

Adding numbers with JavaScript is incredibly easy. All you need to do is use the + operator. For example:


let a = 5;
let b = 6;

let c = a + b;

console.log(c); // 11

In the above example, we are declaring two variables, a and b. a is equal to 5 and b is equal to 6. Then, we declare a third variable, c, which is equal to the sum of a and b. Finally, we log the value of c to the console and it prints out 11.

You can also add multiple numbers together. For example:


let a = 5;
let b = 6;
let c = 10;

let d = a + b + c;

console.log(d); // 21

In this example, we are declaring three variables, a, b, and c. a is equal to 5, b is equal to 6, and c is equal to 10. Then, we declare a fourth variable, d, which is equal to the sum of a, b, and c. Finally, we log the value of d to the console and it prints out 21.

You can also add strings and numbers together. For example:


let a = "Hello ";
let b = "World";
let c = 5;

let d = a + b + c;

console.log(d); // Hello World5

In this example, we are declaring three variables, a, b, and c. a is equal to "Hello ", b is equal to "World", and c is equal to 5. Then, we declare a fourth variable, d, which is equal to the sum of a, b, and c. Finally, we log the value of d to the console and it prints out "Hello World5".

In summary, adding numbers with JavaScript is incredibly easy. All you need to do is use the + operator. You can add multiple numbers together and you can add strings and numbers together as well.

Answers (0)