HomeSearch

JavaScript typeof String, Number

Invoke the typeof operator to determine the type of an object. See if a value is a string.
Typeof. With this operator we get a string that indicates the type of a variable. A variable (like an object or array element) can be passed to typeof.
For performance, typeof is a light-weight operation. In the benchmark it appears to execute as fast as a null check in the V8 engine.
A first example. Here we have an array with three elements—a string, a number, and a function. In JavaScript functions too are objects.function

Loop 1: We print the result of typeof to the page with console.log. We see the lowercase type strings.

Loop 2: We use an if, else chain to test the result of typeof. In this way we can test the types of array elements.

JavaScript program that uses typeof // Create test array. var values = ["cat", 100, function(){}]; // Write the result of typeof. for (var i = 0; i < values.length; i++) { console.log("TYPEOF RESULT: " + typeof values[i]); } // Test the elements with typeof. for (var i = 0; i < values.length; i++) { if (typeof values[i] === "string") { console.log("FOUND string"); } else if (typeof values[i] === "number") { console.log("FOUND number"); } else if (typeof values[i] === "function") { console.log("FOUND function"); } } Output TYPEOF RESULT: string TYPEOF RESULT: number TYPEOF RESULT: function FOUND string FOUND number FOUND function
Arrays. For arrays, we cannot use a "typeof array" expression. We must use the Array.isArray method. We can use isArray on literal arrays and arrays created with the Array constructor.Array
JavaScript program that tests for array type var test = [1, 2, 3]; // See if the value is an array. if (Array.isArray(test)) { console.log("ISARRAY"); } if (typeof test === "array") { // This does not work. console.log("ERROR"); } var emptyArray = Array(100); // We can also test an array made with the Array constructor. if (Array.isArray(emptyArray)) { console.log("ISARRAY"); } Output ISARRAY ISARRAY
Performance. Here we try to estimate the performance of typeof. The values array contains three strings. The typeof keyword is use on each element in the array.

Program 1: The typeof operator is used on each string. The result variable is incremented on each element in the array.

JavaScript program that benchmarks typeof var x1 = performance.now(); var result = 0; var values = ["cat", "bird", "frog"]; for (var i = 0; i < 10000000; i++) { for (var x = 0; x < values.length; x++) { if (typeof values[x] === "string") { result++; } } } var x2 = performance.now(); document.title = (x2 - x1) + "/" + result; JavaScript program that benchmarks null check var x1 = performance.now(); var result = 0; var values = ["cat", "bird", "frog"]; for (var i = 0; i < 10000000; i++) { for (var x = 0; x < values.length; x++) { if (values[x]) { result++; } } } var x2 = performance.now(); document.title = (x2 - x1) + "/" + result; Output 86.33 ms typeof values[x] === "string" 91.69 ms values[x]
A typeof analysis. Based on the benchmark, typeof is a good choice for JavaScript programs—it executes fast. Algorithms that use typeof can perform as well as ones that do not.
A summary. With the typeof operator we get a string that tells us an object's type. The typeof operator appears to perform fast in Chrome—about as fast as a null check.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls