Learn about arithmetic, comparison, logical, assignment, and other types of operators in JavaScript used to perform operations on variables and values.
This code snippet demonstrates various operators in JavaScript:
// Arithmetic Operators
let sum = 10 + 5; // 15
let difference = 10 - 5; // 5
let product = 10 * 5; // 50
let quotient = 10 / 2; // 5
let remainder = 10 % 3; // 1
// Comparison Operators
console.log(5 > 3); // true
console.log(5 === "5"); // false
console.log(5 == "5"); // true
// Logical Operators
let a = true, b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false
// Assignment Operators
let x = 10;
x += 5; // x = 15
// Ternary Operator
let age = 20;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // "Adult"
This code demonstrates:
+
, -
, *
, /
, %
, **
==
, ===
, !=
, !==
, <
, >
, <=
, >=
&&
, ||
, !
=
, +=
, -=
, *=
, /=
typeof
, instanceof
, in
, ternary (?
:
)Q1: What is the result of 5 + '5'
in JavaScript?
Answer: B. "55"
Q2: Which operator checks both value and type?
Answer: C. ===
Q3: What does the logical OR (||) operator return?
Answer: A. The first truthy value or the last value if none are truthy
Q4: Which of these is a valid use of the ternary operator?
let x = if (a > b) then a else b
let x = (a > b) ? a : b
let x = a > b ? : a : b
let x = a > b then a else b
Answer: B. let x = (a > b) ? a : b