DEV Community

Bryant Caruthers
Bryant Caruthers

Posted on • Edited on

Interview Questions for the Javascript Developer: Postfix & Prefix Operators, Truthy & Falsy Values, & == vs. ===

In the last article, we looked at hoisting, prototypal inheritance, and the differences between an attribute and a property. You can find that article here. Today, we will continue to prepare for an interview by looking at the differences between postfix and prefix increment/decrement operators, truthy and falsy values, and equality vs. identity operators.

Postfix & Prefix Increment/Decrement Operators

Mailboxes on a brick wall image

Postfix (x++/x--) and prefix (++x/--x) increment/decrement operators are used to either increase or decrease a variable by 1 and return that value. Most of us are familiar with the postfix increment operator i++ found in a For Loop. So if they both increase or decrease a variable by one, what's the difference?

If it is used postfix (x++/x--), then it returns the value before incrementing or decrementing the variable. Check out this example:

If it is used prefix (++x/--x), it returns the value after incrementing or decrementing the variable. Check out this example:

Truthy & Falsy Values

Bowl of candy hearts image

Truthy and falsy are funny sounding words. But, what are they? JavaScript uses type coercion, in a boolean context, on all values. When a value is evaluated in a boolean context, if the value is true it is called a truthy and if it is false it is called a falsy. All values are considered truthy, except for false, 0, empty strings (single quotes, double quotes, and template literals), null, undefined, and NaN, which are all falsy.

Equality vs. Identity Operators

Equal stairs but diffrent colors image

So what's the difference between == and ===? The equality operator (==) compares two values for equality after any necessary type conversion. So 1 == "1" would return true, just the same as 1 == 1 returns true. JavaScript converts the string "1" into a number and then compares it to the number 1.

The identity or strict equality operator (===) compares two values for equality, but there is no type conversion. So the values have to be of an equal type also. So 1 === 1 would return true, whereas 1 === "1" would return false.

I hope that this article has helped you to better understand postfix and prefix operators, truthy and falsy, and the equality and identity operators. I would like to thank all of my readers for the overwhelming support of my first article on Dev.to. I would also like to thank my instructors, Jerome Hardaway, Brad Hankee, and Phil Tenteromano, over at #Vets Who Code for their training, support, and for pushing me to write articles. I wish you all the best of luck on your interviews and stay tuned for more articles in the series over the next few weeks.

Photo Credits (in order of appearance):
Kristina Tripkovic
Obi Onyeador
Greg Jeanneau

Top comments (0)