YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: The YUI Global Object: Check Data Types with `Lang`

Note: This is YUI 3.x. Looking for YUI 2.x?

## The YUI Global Object: Check Data Types with `Lang`

The YUI Global Object includes several useful type-checking methods in the `Lang` object. In addition to the 'isXYZ' type check methods, YUI3 includes Y.Lang.type, which returns a string represting the type of the tested object. Click the "Check" button in each row to evaluate the data.

View example in new window.

Data | isObject | isArray | isFunction | type
--- | --- | --- | --- | ---
`null``` |
`[] or new Array()` |
`{} or new Object()` |
`function Foo() {}` |
`new Foo()` |

## Type Checking with YUI

### Instantiate YUI

<!-- include yui --> <script type="text/javascript" src="../../build/yui/yui.js"></script> YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {example:true}}).use("node", // This method is in the core of the library, so we don't have to use() any // additional modules to access it. However, this example requires 'node'. function(Y) {

### Checking types

In this example, we use a few of the type-checking methods available in `Lang` to test various types of data.

// Test the input using Y.Lang type checking methods var checkType = function (val) { return { 'object' : Y.Lang.isObject(val), 'array' : Y.Lang.isArray(val), 'function': Y.Lang.isFunction(val), 'type' : Y.Lang.type(val) }; };

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings