Language
Besides arithmetic, comparison and logical operators, JavaScript has a group of operators that work with the language itself rather than with numbers or booleans. They check a value's type, test object relationships, create and remove properties and objects, or combine expressions. The main ones are typeof, instanceof, in, delete, new, void, the comma operator and the conditional (ternary) operator.
typeof: what kind of value is this?
typeof returns a string describing a value's type. It is safe even on variables that were never declared, which makes it useful for feature checks.
typeof 42; // "number"
typeof 'hi'; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof 10n; // "bigint"
typeof Symbol('id'); // "symbol"
typeof {}; // "object"
typeof []; // "object" (arrays are objects)
typeof null; // "object" (a long-standing quirk)
typeof function () {}; // "function"
Because typeof null and typeof [] both give "object", use value === null and Array.isArray(value) when you need to tell them apart.
instanceof and in: relationships and keys
obj instanceof Ctor checks whether Ctor.prototype appears in the object's prototype chain. 'key' in obj checks whether a property exists on the object or anywhere up its prototype chain, even if its value is undefined.
class Shape {}
class Circle extends Shape {}
const c = new Circle();
c instanceof Circle; // true
c instanceof Shape; // true
[] instanceof Array; // true
const settings = { theme: undefined };
'theme' in settings; // true, the key exists
settings.theme !== undefined; // false, so this check misses it
'toString' in settings; // true, inherited from Object.prototype
delete settings.theme; // removes the property, returns true
'theme' in settings; // false
const total = (1, 2, 3); // comma operator: evaluates all, returns 3
const label = c instanceof Circle ? 'round' : 'other'; // conditional
delete, new and void
- delete removes a property from an object. It does not free memory directly and cannot remove variables declared with
let,constorvar. - new calls a constructor or class to create a new object instance.
- void evaluates an expression and returns
undefined. You may see it in old links ashref="javascript:void(0)"; a real<button>is the better choice today.
The ternary and comma operators
The conditional operator condition ? a : b is the only operator that takes three operands. It picks a value and is widely used in template literals and JSX. The comma operator evaluates expressions left to right and returns the last one. You mostly see it in for loops, such as for (let i = 0, j = 10; i < j; i++, j--). Elsewhere it tends to hurt readability.
Where these operators mislead you
Using delete on an array element is a common mistake. delete arr[1] leaves an empty hole and does not change length. Use arr.splice(1, 1) to actually remove an item, or filter to make a new array without it.
instanceof can fail across frames or windows, because each one has its own Array constructor. Array.isArray() works everywhere, so prefer it for arrays.
And remember that in includes inherited keys. To check only the object's own properties, use Object.hasOwn(obj, 'key').
Quick reference
| Operator | Returns |
|---|---|
typeof x | A type name string |
x instanceof C | Boolean, based on the prototype chain |
'k' in obj | Boolean, own or inherited key |
delete obj.k | Boolean; removes the property |
void expr | undefined |
a ? b : c | b or c |
In practice, reach for typeof for primitives, Array.isArray for arrays, Object.hasOwn for own keys and splice rather than delete on arrays.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.