Saturday, July 13, 2013

Is javascript empty array true or false

Not to be accused of plagiarism I would like to point out this stackoverflow link first.

Now, coming to the topic, let me start by asking two questions. In Javascript, what do you think the output will be for the following two code snippets?

1. First code snippet

2. Second code snippet

Before looking at the solutions, take 10 seconds to think what the output might be. Or even better, try them out in the javascript console in your browser (if it is enabled that is).

If you have tried them out, you know the answers. For others, they are below:

1. It would print "empty array is truthy"
2. It would print "empty array == false is true"!!!

That was very confusing to me when I first came to know of it. Then on digging deeper, here's what I found out. Basically, there are two concepts.

First, "if" in javascript would check if the enclosed condition/object is one of the following:
  • false (the false object itself, not just if it is == false, look at the example below)
  • undefined
  • null
  • 0
  • NaN
  • (empty string)
To understand the first bulleted point, a Boolean false object (that is new Boolean(false)) will also evaluate to true in an if condition. Try out the following code: 

For an expression within the "if" condition, remember that the expression will return a value (even if undefined). And "if" operator would work according to that value. If that value is any one in the list above, it would skip the curly brackets code.

Getting back to the first example, an empty array is none of the items in the list above. So it is true. And so javascript would go ahead to evaluate the curly brackets code.

Secondly, the == operator. That would try to convert the array to its string representation by calling the toString method. Why? Take a look at Mozilla javascript reference for comparison opertators.

So, [].toString will evaluate to "" (empty string). Which is equal to false as per the non-strict comparator (==) that we are using. And so in the second example also the code in the curly brackets will be evaluated.

Hope that helps to clarify an empty array seems to act mysteriously.

References:

3 comments:

  1. Each intricate chess piece is handcut by a Meilleur Ouvrier de France to ensure you|to make sure you} receive a unique work of art. The board itself is made from rare and exotic woods with the traditional alternating colored squares finished to a excessive gloss. The timeless art of the double six is reinvented 벳익스플로어 by Dutch wunderkind designer Marcel Wanders in gorgeous mouth-blown black crystal. The dominos are enhanced with gilding and securely stored in elegant, creamy white inlaid marble. The Candy Light wall mild, designed by Jaime Hayon, includes a lampshade in Juvisy-cut crystal and a white or shiny chrome body.

    ReplyDelete
  2. Neighbors bets are often put on in mixtures, for instance "1, 9, 14, and the neighbors" is a 15-chip bet overlaying 18, 먹튀검증 abc-1111 22, 33, 16 with one chip, 9, 31, 20, 1 with two chips and 14 with three chips. The sum of all the numbers on the roulette wheel is 666, which is the "Number of the Beast". "Gwendolen on the roulette desk" – 1910 illustration to George Eliot's "Daniel Deronda". Simply open the classes or tasks and click on} on one of the checkboxes to toggle by way of its state. If you need to save of|to keep away from wasting} your preferences to your account, choose the My Account pre-set. Don't overlook to use any modifications you make after adjusting the filters.

    ReplyDelete
  3. Ziabari and his staff developed a deep-learning framework that quickly supplies a clearer, more accurate reconstruction and an automated evaluation. He will present the process his staff developed through the Institute of Electrical and Electronics Engineers International Conference on Image Processing in October. The framework is already being integrated into software utilized by commercial partner ZEISS within its machines at DOE’s Manufacturing Demonstration Facility at ORNL, the place companies hone 3D-printing methods. ParametricArchitecture is an internet platform that showcases the game-changing capabilities of parametric design and computational tools. Use of this site constitutes acceptance of our User Agreement and Privacy Policy and Cookie Statement and Your California Privacy Rights. PA may earn a portion of sales Hooded Puffer Jackets from merchandise which might be} bought via our site as a part of} our Affiliate Partnerships with retailers.

    ReplyDelete