JavaScript Course
JavaScript
/
Beginner

Operators & Comparisons

Definition

Symbols or keywords that tell the JavaScript engine to perform mathematical, relational, or logical operations.

Explain Like I'm New

Operators are the verbs of programming. If variables are the nouns (like '5' or '10'), the operators are the actions (like '+', '-', '>').

Real World Example

Using the logical AND `&&` operator to conditionally render a button: `isLoggedIn && <button>Logout</button>`.

Common Use Cases

  • •Math calculations
  • •Conditional logic
  • •Assigning values

Terminal Output

bash / terminal
console.log('5' + 3); // '53' console.log('5' - 3); // 2 console.log(typeof null); // 'object' // Double tilde ~~ is a fast shorthand for Math.floor() on positive numbers console.log(~~4.9); // 4

Interview Questions

basic

  • What is the difference between `+` and `-` when used with strings?

intermediate

  • What does the typeof operator return for `null`?

advanced

  • How does the bitwise NOT operator `~` work and what is `~~` used for?

Flash Cards

Question

What is the difference between + and - with strings?

Click to reveal answer
Answer

The `+` operator is heavily overloaded. If either operand is a string, it concatenates them (`'5' + 1 = '51'`). However, the `-` operator only works for math. If it sees a string, it tries to convert it to a number first (`'5' - 1 = 4`).

Question

What does typeof null return?

Click to reveal answer
Answer

It returns `'object'`. This is a famous, unfixable bug in JavaScript dating back to its creation. `null` is a primitive type, not an object, but fixing it now would break millions of legacy websites.