Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 156 · Operators

More Operators and Precedence

Learn JavaScript's remaining operators – typeof, instanceof, in, delete, void and the comma – plus a clear precedence table and the bugs it explains.

You've now met the big operator families: arithmetic, comparison, logical, nullish and optional chaining and bitwise. A few operators don't fit any of those groups. Some are words rather than symbols, like typeof and delete, and one is just a comma.

This lesson rounds off the chapter. First you'll learn what each remaining operator does and when it's useful. Then you'll see the whole precedence table in one place, learn how to read it, and look at the handful of precedence bugs that catch real developers.

typeof: what kind of value is this?

typeof value returns a string naming the value's type. You saw it briefly in Data Types; here are the full results, including the famous oddities:

JS Every typeof result

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Three things to remember:

  • typeof null is "object". It's a mistake from the first version of JavaScript that can never be fixed without breaking old websites. To check for null, write value === null.
  • Arrays are "object". Use Array.isArray(value) to detect them.
  • Functions get their own answer, "function", even though they are objects too. That makes typeof callback === "function" the standard way to check that something can be called.

typeof is also the one safe way to ask about a name that may not exist at all, such as a browser-only global in code that also runs in Node.js: typeof window !== "undefined". (For let and const variables used before their declaration line, even typeof throws, because of the temporal dead zone you met in Variables.)

instanceof: was it made by this class?

value instanceof Constructor checks whether an object was created by a constructor or class, or by one that inherits from it. You'll understand exactly how it works after Prototypes; for now, think of it as "is this object a kind of X?"

JavaScript
const now = new Date();
now instanceof Date;       // true
now instanceof Object;     // true — every Date is also an Object

[] instanceof Array;       // true
"text" instanceof String;  // false — primitives are not objects

instanceof is most useful for objects you create from your own classes and for telling error types apart in a catch block. For arrays, prefer Array.isArray(): instanceof Array can fail for arrays that come from another window or iframe, because each one has its own Array constructor.

in: does this property exist?

"key" in object returns true if the object has a property with that name, including inherited ones, even if the property's value is undefined. That last part is the difference from simply reading the property:

JS The in operator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Use in when "the property exists but is empty" and "the property doesn't exist" mean different things. Use Object.hasOwn(obj, key) when inherited properties should not count. You'll learn more about both in Working with Objects.

delete: remove a property

delete object.key removes a property from an object and returns true. Afterward the property is gone completely, so in returns false. Setting a property to undefined is different: the key still exists.

JavaScript
const user = { name: "Ana", tempToken: "x81" };

delete user.tempToken;
"tempToken" in user;     // false

A few rules:

  • delete only works on properties, never on variables. delete count is a SyntaxError in strict mode, which modules and classes always use.
  • Don't use it on arrays. delete list[1] leaves an empty hole and doesn't change length. Use splice() or filter() from Array Methods.
  • Prefer copying to deleting when other code shares the object. Rest destructuring makes a copy without the unwanted key: const { tempToken, ...safeUser } = user; (see Destructuring).

void, the comma and grouping

These three are small, but you'll meet all of them when reading code.

void expression evaluates the expression and then returns undefined, whatever the result was. Old code wrote void 0 as a way to get undefined, and old HTML links used href="javascript:void(0)"; use a <button> for that today. One modern use remains: in an arrow function, () => void save() makes it clear the function returns nothing.

The comma operator evaluates expressions left to right and returns the last one. You'll almost only see it in the update part of a for loop that moves two counters:

JavaScript
for (let left = 0, right = word.length - 1; left < right; left++, right--) {
  // left++, right-- is the comma operator: both run each round
}

Not every comma is the comma operator. The commas in function arguments, array literals, object literals and let a = 1, b = 2 are just separators. Outside loops, avoid the comma operator; two statements are clearer.

Grouping parentheses ( … ) make the part inside evaluate first, overriding precedence. They don't change the order in which operands are evaluated, though: JavaScript still works through the expression from left to right.

JS void, the comma and evaluation order

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Finally, the three dots ... are not an operator, even though they look like one. They are special syntax for spreading and collecting values, allowed only in arrays, objects, function calls and parameters. They're covered in Spread and Rest.

Operator precedence

Precedence decides which operator applies first when an expression mixes several. Associativity decides the order when the same level appears twice in a row: most operators group left to right, so 10 - 3 - 2 is (10 - 3) - 2. Here is the table, from the tightest binding to the loosest:

Level Operators Groups
1 (highest) ( … ) grouping –
2 ., ?., [ ], calls f(), new X() left to right
3 postfix x++, x-- –
4 !, ~, unary +/-, prefix ++/--, typeof, void, delete, await right to left
5 ** right to left
6 *, /, % left to right
7 +, - left to right
8 <<, >>, >>> left to right
9 <, <=, >, >=, in, instanceof left to right
10 ==, !=, ===, !== left to right
11 & left to right
12 ^ left to right
13 | left to right
14 && left to right
15 ||, ?? left to right
16 ? :, =, += and all other assignments, => right to left
17 (lowest) , (comma operator) left to right

You don't need to memorize it. The broad shape is enough: access and calls → unary → math → comparisons → bitwise → logical → conditional and assignment → comma. Right-to-left grouping is what lets a = b = 0 assign to both variables, and makes 2 ** 3 ** 2 mean 2 ** (3 ** 2), which is 512.

JS Precedence and associativity in action

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Precedence bugs to avoid

Most precedence bugs share one cause: a unary operator or a low-precedence operator grabbing less, or more, than you meant. These are the ones you're most likely to write:

JS Classic precedence bugs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two more mixes are so error-prone that JavaScript refuses them outright with a SyntaxError, forcing you to add parentheses:

JavaScript
-2 ** 2;        // SyntaxError: write (-2) ** 2 or -(2 ** 2)
a ?? b || c;    // SyntaxError: write (a ?? b) || c or a ?? (b || c)

The real rule: if you have to look something up in the precedence table, the next reader will too. Add parentheses, or split the expression into well-named variables.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does typeof null return?

    It's a long-standing quirk kept for compatibility. Check for null with value === null.
  2. 2. Given const list = ["a", "b"], what is "a" in list?

    For arrays, in asks whether an index exists. 0 in list is true; use list.includes("a") to look for a value.
  3. 3. What does 2 ** 3 ** 2 evaluate to?

    ** groups right to left, so it means 2 ** (3 ** 2), which is 2 ** 9.
  4. 4. Which line correctly checks that key is NOT a property of obj?

    ! binds tighter than in, so !key in obj negates the key first. Wrap the in test in parentheses. !in and not are not JavaScript operators.
  5. 5. What does const r = (1, 2, 3); store in r?

    The comma operator evaluates each expression from left to right and returns the last one.

Summary

  • typeof returns a type name as a string; watch for typeof null === "object" and arrays being "object". It's safe on undeclared names.
  • instanceof checks whether an object was made by a class or constructor; prefer Array.isArray() for arrays.
  • in checks whether a property exists, including inherited ones; with arrays it checks indexes. Object.hasOwn() checks own properties only.
  • delete removes object properties; don't use it on variables or arrays.
  • void returns undefined; the comma operator returns its last operand; ... is syntax, not an operator.
  • Precedence runs roughly: access and calls → unary → math → comparison → bitwise → logical → conditional and assignment → comma. **, assignment and ? : group right to left.
  • When in doubt, add parentheses: they fix the classic bugs with !, &, ? : and object-returning arrows.

Next: Conditionals: if, else if and else — using everything from this chapter to make your programs choose between paths.