DONATE  
MENU
AJAX
JSON
MYSQL
JAVASCRIPT
HTML
CSS
PHP

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Numbers
Share Tweet WhatsApp

JavaScript Numbers

A JavaScript number is any positive or negative integer, float, double, short, long and yes, just any mathematically correct number you may think of. Unlike, some statistically typed programming languages, JavaScript does not require you to specify if a number is an integer or float or double. Once a number is mathematically correct, it is a JavaScript number and can be used in arithmetic operations.
1;
109;
2.00;
3.45;
1E5; //100000
2e6; //2000000
1E-5; //0.00001
2e-6; //0.000002
-1;
-109;
-2.00;
-3.45;
-1E5; //-100000
-2e6; //-2000000
-1E-5; //-0.00001
-2e-6; //-0.000002
NaN The JavaScript NaN stands for "Not a Number". It is a reserved keyword that is returned when a number is not mathematically correct or not acceptable in JavaScript. For example, if you multiply a number with a string, "NaN" is returned.
<script>
alert(2 * "two");
alert(4 - "two");
</script>

Copy PRACTICE IT

Number Object Though numbers are primitive data types yet there exists an object that can be used in creating or manipulating them. Below is how a number can be created using the Number object prototype.
<script>
var num = new Number(134);
alert(num);

num = new Number(1e5);
alert(num);
</script>

Copy PRACTICE IT

As you can see, creating numbers explicitly e.g 124 is easier and better than using the Number object prototype. You can manipulate numbers whether created explicitly or implicitly using the properties or methods of the Number object. For example, you can use the toString() method to convert numbers, which are originally hexadecimal, to any base.
<script>
var num = 5; //Hexadecimal or base 10
alert(num.toString(2)); //converts to binary/base 2
alert(num.toString(8)); //converts to octal or base 8
</script>

Copy PRACTICE IT

Number's typeof() Using the typeof() operator on a number or variable containing a number (including NaN) will return number. Therefore, to test if a value is a number or not, use the typeof() operator on the value.
<script>
var num = 50;
alert(typeof(num));
</script>

Copy PRACTICE IT

You will learn more about the number object properties and methods in the next chapter. SUMMARY
  1. A JavaScript number is any positive or negative integer, float or double.
  2. Unlike strictly typed programming languages, in JavaScript, you don't need to further specify what kind(integer, float, double, short, long) is a number.
  3. NaN is a JavaScript reserved keyword that means a number is not valid or acceptable in JavaScript.
  4. Though primitive, JavaScript numbers have an object.
  5. We can use the object to create new numbers or use its properties and methods to manipulate numbers created explicitly or implicitly.
  6. The typeof operator when used on numbers will return number.
Take a short quiz on JavaScript Numbers.
Ask a question or tell us if JavaScript Numbers is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode