Skip to content
the poor coder

JavaScript Number

Poor Man
Published
Reading time
1 min read
On this page

JavaScript Number#

In JavaScript, number is a primitive data type that represents numeric values. It can be positive, negative, or zero. Numbers can be written with or without decimals.

Creating a Number#

There are multiple ways to create a number in JavaScript. The simplest way is to just write a number:

JavaScript
let myNumber = 42;

You can also create a number using the Number() function:

JavaScript
let myNumber = Number("42");

Or by using the parseInt() function to convert a string to an integer:

JavaScript
let myNumber = parseInt("42");

Mathematical Operations#

JavaScript provides a variety of mathematical operations that can be performed on numbers. Here are some examples:

  • Addition: let sum = 4 + 5;
  • Subtraction: let difference = 10 - 3;
  • Multiplication: let product = 6 * 7;
  • Division: let quotient = 12 / 4;
  • Remainder: let remainder = 10 % 3;
  • Exponentiation: let result = Math.pow(2, 3); // 2 raised to the power of 3

NaN and Infinity#

JavaScript also has two special numeric values: NaN and Infinity.

NaN stands for "Not a Number" and is returned when a mathematical operation fails. For example:

JavaScript
let result = 0 / 0; // NaN

Infinity represents the mathematical concept of infinity. It is returned when a number is too large to be represented by JavaScript. For example:

JavaScript
let result = 1 / 0; // Infinity

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC