spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

What is the difference between var num=30 and var num=new Number(30) in JavaScript?


The first creates a primitive. The other an object.

In theory there is a difference but in practice none. The JavaScript engine automagicly boxes a primitive to an object when it needs to be an object.

var myPrimitiveNumber = 42;
// calling .toFixed will 'box' the primitive into
a number object,
// run the method and then 'unbox' it back to a
primitive
console.log( myPrimitiveNumber.toFixed(2) );

The only usage I've found is if you want to return a primitive from a constructor function.

function Foo() {
    return 42;
}

var foo = new Foo();
console.log(foo); // foo is instance of Foo

function Bar() {
    return new Number(42);
}

var bar = new Bar();
console.log(bar); // bar is instance of Number

Categories : Javascript

Related to : What is the difference between var num=30 and var num=new Number(30) in JavaScript?
Why is an Array with one Number a Number in Javascript?
isNaN coerces its value to a number. (See MDN) Because the string representation of an array is all of its items concatenated with a comma. And the numerical representation of that is NaN because of the comma. But if there's only one item, hence no comma, it's able to be converted to a number.

Categories : Javascript
Difference between javascript class declaration
This should work fine as long as you actually call the constructor and create an object which is how you've configured the Example code: var Example = require("./example"); var item = new Example(); item.run();

Categories : Javascript
how to display date difference using javascript and disable old dates
You can check here in jsfiddle giving days difference and printing in div down to datepicker combined past disabling alone and date calculation dateTo gets enabled when you select dateFrom HTML file <form method="post"> <div height="100px"> <br/> From: <input type="text" name="date_from" id="TxtFromDate" autocomplete="off" /> To: <input type="text" n

Categories : Javascript
Get date given year, week number, day number within week with JavaScript
I would highly recommend you to use the moment.js library. This is a complete date manipulation library. Your problem would be solved by this moment().year(2014).week(47).day(3).toDate();

Categories : Javascript
PHP Code for printing the difference between the number of days from Date of Birth to the current date
See the DateTime: $born_date = new DateTime('25-02-2014'); $diff = $born_date->diff(new DateTime()); echo $diff->days.' days'; // returns '272 days'

Categories : PHP
Recently Add
How to get selected text with JavaScript?
Non Modal Alert Messages in java script, activated by a button
How to Count [Combined] line length of multiple files?
Is there any javascript event fired when the on-screen keyboard on mobile safari or chrome opens?
Datatables sort by time ago
Internet Explorer getClientRects absolute positioned elements
How can I transform a CamanJS Filter into a KineticJS Filter?
AngularJS : index not updating after removing array item(s) by index
Slick slideshow in ember.js template
Recursive function with defer
Unable to find module mongodb in simple node.js app
Why does function return false after checking only one value in loop
Detect if the mouse is over an element
Placing and rotating svg elements along a path with Snap.svg
YouTube Asynchronous function
Cordova App: Stop JavaScript from running when App is closed
how to define a variable in an javascript ifstatement
display dd on click using jquery
jQuery Timer Countdown within Session
setInterval on click event to run for 10 seconds
Are syntax errors raised in JavaScript during Tokenizing or Parsing?
Can i perform form based http authentication for my TP-Link Wireless Router(IP: 192.168.0.1)
Blocked frame in reCapcha
Updated property not recognized by $watch
Template Missing error on update only
nodejs object declaration and immediate shorthand if statement crashes the app
Hide / show dynamically generated table columns with right / left animation
Convert UTC Date to Local Date in JavaScript
Position DOM elements without CSS
modifying mergesort to count inversions in JavaScript
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.