JavaScript(JS) JS add two numbers
In JavaScript, you can add two numbers using the +
operator. Here's an example:
let num1 = 5; let num2 = 10; let result = num1 + num2; console.log(result); // Output: 15
In the example above, we declare two variables num1
and num2
with the values 5
and 10
, respectively. We then add the two numbers together using the +
operator and store the result in a new variable result
. Finally, we log the value of result
to the console, which outputs 15
.
Note that the +
operator can also be used to concatenate two strings together, so if you're working with strings that look like numbers, be sure to convert them to numbers first using the parseInt()
or parseFloat()
function. For example:
let str1 = "5"; let str2 = "10"; let result = parseInt(str1) + parseInt(str2); console.log(result); // Output: 15
In the example above, we use the parseInt()
function to convert the strings "5"
and "10"
to the numbers 5
and 10
, respectively, before adding them together with the +
operator.