Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Last active September 10, 2015 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltukalakus/a4f193566c62446d1424 to your computer and use it in GitHub Desktop.
Save saltukalakus/a4f193566c62446d1424 to your computer and use it in GitHub Desktop.
typeof vs instanceof
/*
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