Format Hexadecimal Number in JavaScript

Giri Raju
Updated on 17-Jun-2020 06:35:28

2K+ Views

Firstly convert your number to hexadecimal with leading zeros and then format it by adding dashes.ExampleYou can try to run the following code to format hexadecimal number −Live Demo                    var num = 32122;          var myHex = ("000000000000000" + num.toString(16)).substr(-16);          var resHex = myHex.substr(0, 8)+'-'+myHex.substr(8,4)+'-'+myHex.substr(12,4);          document.write(resHex);           Output00000000-0000-7d7a

Get the Length of a Number in JavaScript

Vrundesha Joshi
Updated on 17-Jun-2020 06:34:53

19K+ Views

Use the toString() method to covert the Number to string, then the length() method gives you length.ExampleYou can try to run the following code to learn how to get the length of a Number −Live Demo                    var num1 = 344;          var num2 = 149856;          document.write("Length of number "+num1+" = "+ num1.toString().length);          document.write("Length of number "+num2+" = "+ num2.toString().length);           OutputLength of number 344 = 3 Length of number 149856 = 6

Check if a Number is NaN in JavaScript

Krantik Chavan
Updated on 17-Jun-2020 06:30:40

291 Views

NaN is a JavaScript property, which is Not-a-Number value. This shows it is not a legal number. Here’s the syntax −SyntaxNumber.NaNTo find out whether a number is NaN, use the Number.isNaN() or isNan() method. Here’s an example to check −ExampleLive Demo           Check                      function display() {             var a = "";             a = a + isNaN(434) + ": 434";             a = a + isNaN(-23.1) + ": -23.1";             a = a + isNaN('Hello') + ": 'Hello'";             a = a + isNaN(NaN) + ": NaN";             a = a + isNaN('') + ": ''";             a = a + isNaN(0) + ": 0";             a = a + isNaN(false) + ": false";             document.getElementById("test").innerHTML = a;          }          

Calculate Difference Between Two Dates in JavaScript

Jennifer Nicholas
Updated on 17-Jun-2020 06:27:09

2K+ Views

To get dates in JavaScript, use the getTime() method. Forgetting the difference between two dates, calculate the difference between date and time.ExampleYou can try to run the following code to learn how to calculate a difference between two dates −Live Demo                    var dateFirst = new Date("11/25/2017");          var dateSecond = new Date("11/28/2017");          // time difference          var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());          // days difference          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));          // difference          alert(diffDays);          

Get Number Value of a String in JavaScript

Nancy Den
Updated on 17-Jun-2020 06:24:14

540 Views

To get an integer in the string “abcde 30”, use the following regex in JavaScript, else it will return NaN. With that, use parseInt() to get the numbers with regex under String match() method −ExampleLive Demo                    var myStr = "abcdef 30";          var num = parseInt(myStr.match(/\d+/))          alert(num);          

Get String Representation of a Number in JavaScript

Daniol Thomas
Updated on 17-Jun-2020 06:23:34

487 Views

Use the toString() method to get the string representation of a number. You can try to run the following code to print a string representation of numbers like 20, -40, 15.5 −ExampleLive Demo                    var num1 = 25;          document.write(num1.toString()+"");          document.write((30.2).toString()+"");          var num2 = 3;          document.write(num2.toString(2)+"");          document.write((-0xff).toString(2));           Output25 30.2 11 -11111111Above, you can see we added a parameter to the toString() method. This is an optional parameter, which allows you to add an integer between 2 and 36, which is the base for representing numeric values.

Maximum Value Represented by Number Object in JavaScript

Smita Kapse
Updated on 17-Jun-2020 06:20:45

325 Views

The largest possible value a number in JavaScript can have 1.7976931348623157E+308. The Number.MAX_VALUE property belongs to the static Number object. It represents constants for the largest possible positive numbers that JavaScript can work with.ExampleYou can try to run the following code to get the maximum value represented by Number object −Live Demo                                         Click the following to see the result:                          

Use the 'with' Keyword in JavaScript

Nitya Raut
Updated on 17-Jun-2020 06:14:29

6K+ Views

The with keyword is used as a kind of shorthand for referencing an object's properties or methods.The object specified as an argument to with becomes the default object for the duration of the block that follows. The properties and methods for the object can be used without naming the object.SyntaxThe syntax for with object is as follows −with (object){    properties used without the object name and dot }ExampleYou can try to learn the following code to learn how to implement with keyword −Live Demo           User-defined objects               ... Read More

Difference Between New Operator and Object Constructor in JavaScript

Rishi Rathor
Updated on 17-Jun-2020 06:09:54

1K+ Views

The new operatorThe new operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor method.In the following example, the constructor methods are Object(), Array(), and Date(). These constructors are built-in JavaScript functions.var department = new Object(); var books = new Array("C++", "Perl", "Java"); var day = new Date(“December 1, 2017");The object() constructorA constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable.The variable contains ... Read More

Pop Up Print Dialog Box Using JavaScript

Amit Sharma
Updated on 17-Jun-2020 06:09:24

4K+ Views

To pop-up a print dialog box using JavaScript, use the print() method. With the dialog box, you can easily set the printing options like which printer to select for printing.This is the dialog box −ExampleYou can try to run the following code to learn how to print a page −Live Demo           Click to Print                function display() {             window.print();          }          

Advertisements