Back to articles
JavaScript Part 3: Operators, Arithmetic, Assignment & Comparisons

JavaScript Part 3: Operators, Arithmetic, Assignment & Comparisons

via Dev.to BeginnersKarthick Narayanan

An operator is a symbol that performs an operation on one or more values. Think of operators as the verbs of JavaScript — they make things happen. 1. JS Arithmetic Operators Arithmetic operators do math . Operator Name Example Result + Addition 10 + 5 15 - Subtraction 10 - 5 5 * Multiplication 10 * 5 50 / Division 10 / 5 2 % Modulus (remainder) 10 % 3 1 ** Exponentiation 2 ** 4 16 ++ Increment x++ adds 1 -- Decrement x-- subtracts 1 Basic examples let a = 20 ; let b = 6 ; console . log ( a + b ); // 26 console . log ( a - b ); // 14 console . log ( a * b ); // 120 console . log ( a / b ); // 3.3333... console . log ( a % b ); // 2 (remainder when 20 is divided by 6) console . log ( a ** 2 ); // 400 (20 squared) Modulus % The % operator gives you the remainder after division. It's surprisingly useful! console . log ( 10 % 3 ); // 1 → because 10 = (3 × 3) + 1 console . log ( 15 % 5 ); // 0 → because 15 divides evenly by 5 💡 A common use: checking if a number is even or odd. let num = 8 ;

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
7 views

Related Articles