Last active
September 10, 2015 12:26
-
-
Save saltukalakus/a4f193566c62446d1424 to your computer and use it in GitHub Desktop.
typeof vs instanceof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
typeof is a construct that "returns" the primitive type of whatever you pass it. | |
instanceof tests to see if the right operand appears anywhere in the prototype chain of the left. | |
*/ | |
var test = require('tape'); | |
test('typeof vs instanceof tests', function (t) { | |
t.plan(13); | |
t.equal(typeof Date.now, 'function'); | |
t.equal(Date.now instanceof Function, true); | |
t.equal(Date.now instanceof Object, true); | |
var test_string1 = 'test string'; | |
t.equal(typeof test_string1, 'string'); | |
t.equal(test_string1 instanceof String, false); | |
var test_string2 = new String("test 2 string"); | |
t.equal(typeof test_string2, 'object'); | |
t.equal(test_string2 instanceof String, true); | |
t.equal(test_string2 instanceof Array, false); | |
t.equal(test_string2 instanceof Object, true); | |
var test_array1 = [1, 2, 3]; | |
t.equal(typeof test_array1, 'object'); | |
t.equal(test_array1 instanceof Object, true); | |
t.equal(test_array1 instanceof Array, true); | |
t.equal(test_array1 instanceof String, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment