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.
x--) and prefix (
--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--), then it returns the value before incrementing or decrementing the variable. Check out this example:
If it is used prefix (
--x), it returns the value after incrementing or decrementing the variable. Check out this example:
0, empty strings (single quotes, double quotes, and template literals),
NaN, which are all falsy.
So what's the difference between
===? The equality operator (
==) compares two values for equality after any necessary type conversion. So
1 == "1" would return
true, just the same as
"1" into a number and then compares it to the number
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
1 === "1" would return
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.