Sum of Two Numbers

Program to add two numbers

JavaScriptBeginner
JavaScript
// Adding two numbers
let num1 = 10;
let num2 = 20;
let sum = num1 + num2;

console.log("Sum of", num1, "and", num2, "is:", sum);

// Using template literal
console.log(`Sum of ${num1} and ${num2} is: ${sum}`);

Output

Sum of 10 and 20 is: 30
Sum of 10 and 20 is: 30

This program demonstrates basic arithmetic operations in JavaScript.

Arithmetic Operators

JavaScript supports standard arithmetic operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)
  • **: Exponentiation (power)

Number Data Type

JavaScript has one number type that handles both integers and floating-point numbers:

javascript
let integer = 42;
let decimal = 3.14;
let negative = -10;

Variable Assignment

The = operator assigns a value to a variable:

javascript
let result = num1 + num2;

Order of Operations

JavaScript follows standard mathematical order:

  1. Parentheses
  2. Exponentiation
  3. Multiplication/Division
  4. Addition/Subtraction

Example:

javascript
let result = 2 + 3 * 4; // 14, not 20
let result2 = (2 + 3) * 4; // 20

Type Coercion

JavaScript automatically converts types when needed:

javascript
"5" + 3; // "53" (string concatenation)
"5" - 3; // 2 (number subtraction)

Be careful with type coercion - use explicit conversion when needed!