JavaScript Operators - Exercise 1

In JavaScript, operators perform an operation on one or more operands, such as values or variables. For example, total = price * quantity multiplies two operands and assigns the result.

Operators are grouped by the kind of work they do, such as arithmetic, assignment, comparison, logical, bitwise, unary, and conditional operations. For example, score += 5 combines arithmetic with assignment.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Unary operators
  • Ternary (conditional) operator

Arithmetic operators calculate with numeric values, including addition, subtraction, multiplication, division, remainder, and exponentiation. For example, const area = width * height calculates a rectangle's area.

The + operator adds numbers, but when either operand is a string it concatenates values as text. For example, 2 + 3 is 5, while 'Hi, ' + 'Ada' is 'Hi, Ada'.

The assignment operator (=) evaluates the expression on its right and stores that value in the variable on its left. For example, let count; count = 3; initializes count.

Use = to assign a new value, and use compound forms such as += when updating a value. For example, let x = 10; x += 2; leaves x equal to 12.

javascript

var x = 10;

Comparison operators compare values and normally return true or false. For example, age >= 18 is true when an adult age is stored in age.

The loose equality operator (==) allows type conversion before comparing values. For example, 5 == '5' is true, but prefer 5 === '5' when that conversion is not intentional.

Strict equality (===) returns true only when both operands have the same value and type; it does not coerce strings into numbers. Thus, 5 === 5 is true but 5 === '5' is false.

The logical AND operator (&&) evaluates the second operand only when the first is truthy. For example, isMember && hasTicket is true only when both conditions are truthy.