<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<!-- disregard this pane, html is shown in the "result" pane -->
<h4><strong>← Code Example from <a href="http://www.javascriptenlightenment.com/" target="_blank">JavaScript Enlightenment Book</a> by <a href="http://www.codylindley.com" target="_blank">Cody Lindley</a></strong></h4>
<p>JavaScript code is executed on page load. Click the "Run" button in the top bar to re-run the JavaScript code. Output is logged to the <a href="http://getfirebug.com/firebuglite" target="_blank">Firebug Lite</a> console below ↓.</p>
JavaScript
// no object is created when producing primitive values, notice no use of the "new" keyword
var primitiveString1 = "foo";
var primitiveString2 = String('foo');
var primitiveNumber1 = 10;
var primitiveNumber2 = Number('10');
var primitiveBoolean1 = true;
var primitiveBoolean2 = Boolean('true');
//confirm the typeof is not object
console.log(typeof primitiveString1, typeof primitiveString2); //logs 'string,string'
console.log(typeof primitiveNumber1, typeof primitiveNumber2); //logs 'number,number,
console.log(typeof primitiveBoolean1, typeof primitiveBoolean2); //logs 'boolean,boolean'
// versus the usage of a constructor and new keyword for creating objects
var myNumber = new Number(23);
var myString = new String('male');
var myBoolean = new Boolean(false);
var myObject = new Object();
var myArray = new Array('foo', 'bar');
var myFunction = new Function("x", "y", "return x * y");
var myDate = new Date();
var myRegExp = new RegExp('\\bt[a-z]+\\b');
var myError = new Error('Crap!');
// logs 'object object object object object function object function object'
console.log(
typeof myNumber,
typeof myString,
typeof myBoolean,
typeof myObject,
typeof myArray,
typeof myFunction, // BE AWARE typeof returns function for all function objects
typeof myDate,
typeof myRegExp, // BE AWARE typeof returns object for RegExp()
typeof myError
);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.