Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
Pluralsight + Digital-Tutors - 3,000 tech & creative courses - starting at $29/month Get it now →
June 12, 2013

23 Different Values vs. Underscore.js is* Functions

By

Why do I like Underscore.js? Because it makes working with JavaScript easy!

A common task in any code is determining “what is what”. In this case Underscore.js has a number of functions to help you determine between, objects, arrays, undefined, null values and a whole lot more. I’m calling the collection of those functions the ‘is*’ functions as they are named isEmpty, isElement, isArray and so on.

The following tables depict how each of the Underscore is* functions evaluate against a series of values which represent many of the types of values you regularly encounter in JavaScript.

underscore-is-star-functions-1

underscore-is-star-functions-2

Taking a Look at the Results

For the most part the functions behave against values just as you might expect, but there are a few anomalies which warrant some further explanation.

isEmpty

The isEmpty function seems to return some false positives. Reviewing the output, an empty object, empty array and empty string are correctly considered empty by isEmpty returning true, but there are some other values that don’t really make sense. For instance it may seem odd that integers, floating point numbers, regular expressions are considered empty by this function.

The isEmpty function is only appropriate for use with objects an arrays. According to the developers, you shouldn’t use it on strings or numbers and apparently Booleans, Dates, Regular Expressions, empty arguments or functions either :)

isObject

In JavaScript nearly everything is considered an object so what’s most interesting about the isObject function is not what is returns true on, but rather what is considers not to be an object. Generally any string or numeric value is not considered an object which of course is appropriate. You just need to take care on how you intend to use this function because if you are trying to draw a distinction between an object as defined as an object literal versus something like an array, you’ll be better off using the isArray function because JavaScript arrays are in fact objects.

Arguments are Arguments, Not Arrays

This next item isn’t anything strange in Underscore.js, but often rather, in our thinking. The arguments object in JavaScript can be a bit deceptive. While you can use an indexer to iterate over function arguments, and it does has a length property, arguments are not an instance of an array. Underscore.js rightly returns false when passing function arguments to the isArray function and false when evaluating an array with isArguments.

Making JavaScript Easy

If you haven’t had the chance I hope you have an opportunity to check out my course Underscore.js Fundamentals where I demonstrate how underscore simplifies working with objects, arrays and functions and how you can improve the expressiveness and performance of your code using Underscore.js.

About the Author


Discussion