All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Creation

Creating a number is easy, it can be done just like for any other variable type using the var keyword.

Numbers can be created from a constant value:

// This is a float:
var a = 1.2;

// This is an integer:
var b = 10;

Or from the value of another variable:

var a = 2;
var b = a;

Basic Operators

You can apply mathematic operations to numbers using some basic operators like:

  • Addition: c = a + b

  • Subtraction: c = a - b

  • Multiplication: c = a * b

  • Division: c = a / b

You can use parentheses just like in math to separate and group expressions: c = (a / b) + d

Advanced Operators

Some advanced operators can be used, such as:

  • Modulus (division remainder): x = y % 2

  • Increment: Given a = 5

    • c = a++, Results: c = 5 and a = 6

    • c = ++a, Results: c = 6 and a = 6

  • Decrement: Given a = 5

    • c = a--, Results: c = 5 and a = 4

    • c = --a, Results: c = 4 and a = 4

Numbers

JavaScript has only one type of numbers – 64-bit float point. It's the same as Java's double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value.

In this chapter, we'll learn how to create numbers and perform operations on them (like additions and subtractions).