“Brief” Guide to JavaScript – Part 7 | Blog of Jeff

“Brief” Guide to JavaScript – Part 7

Utility Functions

Various functions that are useful when you start doing more than simple JavaScript/DOM manipulation code.

Typeof

Sometimes you’ll want to determine what type of data a variable contains, you can use the typeof keyword which returns a string of the type (parenthesizes are optional, but recommended):

typeof('hello world') // => 'string'  typeof 123 // => 'number'  typeof(true) // => 'boolean'  typeof(function(){}) // => 'function'

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

It’s worth mentioning a quirk in typeof with arrays and objects:

typeof([1 2 3]) // => 'object'  typeof({hello: 'world'}) // => 'object'

We’ll talk about why when we cover objects in depth, but arrays are actually objects.

Just released part 7, check it out :)

Posted via web from Jeff Hui | Comment »

Notes