Broad Network


ECMAScript Core Number Basics and Testing

ECMAScript Number Part 1

ECMAScript 6

Foreword: In this tutorial I talk about numbers, and how they can be tested.

By: Chrysanthus Date Published: 15 Jul 2016

Introduction

In ECMAScript, a number can be a whole number, an integer, a real number or a real number in exponential form. In this tutorial I talk about numbers, and how they can be tested.

Pre-Knowledge
This tutorial is part of the volume, ECMAScript Course. At the bottom of this page you have links to the different series you should have read before coming here.

Numbers

Whole Number
A whole number is an integer without the or + sign. It is actually a positive number, (also called an unsigned number). It can be assigned to a variable as in the following statement:

    $wholeNum = 25;

Integer
An integer is a whole number, which can be positive or negative. If it is positive, the + sign can be omitted. It can be called a sign number. It can be assign to a variable as in the following cases:

        $inter1 = -246;
        $inter2 = +36;
        $inter3 = 36;

Real Number
A real number is a number with a decimal point. If it is a negative number, it would have the negative sign. If it is a positive number it might or might not have the + sign. It can be assigned to a variable as in:

    $rl = 2.5;

Note that the decimal part of the number may be zero. In that case it is equivalent to an integer.

Real Number in Exponential Form
A real number can be written in exponential form. For example, 268.3 can be written as,

    2.683e2

You begin with a single digit whole number; then you have an optional decimal point. After, you have an optional decimal part; then e; and finally a number, which indicates the number of places the decimal point has to be shifted to get the real number. If the decimal point has to be shifted to the left, then this number has to be preceded by - . If the complete number is negative, then the complete number has to be preceded by - .

Testing Numbers
First of all let us see how to test if a variable is a string. The following string matching expression, test if a variable (subject) is a string:

    subject.match(/\D/)

This tests if there is a non-digit character in the string. Remember, the match method returns sub-string found (true) for success and null (false) for failure. Try the following code:

        str = "We are the world.";

        if (str.match(/\D/))
            alert('Matched');
        else
            alert('Not Matched');

Whole Number
The following expression will return true if the subject is a whole number:

    subject.match(/^\d+$/)

In the regex, \d means digit. ^ means match at the beginning of the string. + means match the preceding character 1 or more times. $ means the end of the string. Try the following code:

    subject = 25;

    if (subject.match(/^\d+$/))
        alert("It is a whole number.");

Integer
The following expression will return true if the subject is an integer:

    subject.match(/^[+-]?\d+$/)

[+-] means + or -. ? means match the preceding character, 0 or 1 time. Try the following code:

        subject = "-246";

        if (subject.match(/^[+-]?\d+$/))
            alert("It is an integer.");

Real Number
The following expression will return true if the subject is a real number:

    subject.match(/^-?(?:\d+\.?|\.\d)\d*$/)

I leave the explanation of the regex as an exercise for you. Try the following code:

        subject = "2.5";

        if (subject.match(/^-?(?:\d+\.?|\.\d)\d*$/))
            alert("It is a real number.");

Real Number in Exponential Form
The following expression will return true if the subject is a real number in exponential form:

    subject.match(/^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?$/i)

Note that the e in the exponential form can also be E; that is why you have /i at the end of the regex. I leave the explanation of the rest of the regex as an exercise for you. Try the following code:

        subject = "2.683e2";

        if (subject.match(/^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?$/i))
            alert("It is a real number.");

That is it for this part of the series. We continue in the next part.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message