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:
javascriptlet integer = 42; let decimal = 3.14; let negative = -10;
Variable Assignment
The = operator assigns a value to a variable:
javascriptlet result = num1 + num2;
Order of Operations
JavaScript follows standard mathematical order:
- Parentheses
- Exponentiation
- Multiplication/Division
- Addition/Subtraction
Example:
javascriptlet 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!