Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Numbers in JavaScript

User image

Published by

sanya sanya

Published at: 6th May, 2023
3.29 mins read

Several Numeric notations are available in Javascript. Javascript Numbers are always stored as double-precision floating point numbers. In Javascript, there is only one type of numbers and those can be written without decimal and decimal. The example of numbers with decimals and without decimals is as follows -

let x = 4; let y = 3.22;

The numbers with decimals are known as floating precision and the numbers without decimals are comes under Integer Precision.

Numbers and Strings Addition

Four cases arise when adding number and string and all the cases are as follows -

  1. If we add two numbers, the result will be a number.

  2. let x = 4; let y = 1; let z = x + y; console.log(z)
    //Output: 5

  3. If we add two strings, the result will be string concatenation.

  4. let x = "Hello" let y = "World" let z = x + y; console.log(z);
    //Output: HelloWorld

  5. If we add a number and a string, the output will be the string concatenation.

  6. let x = 10; let y = "100"; let z = x + y; console.log(z)
    //Output: 10100

  7. If we add a string and a number, the output will be the string concatenation.

  8. let x = "20"; let y = 20; let z = x + y; console.log(z)
    //Output: 2020

### Javascript Numeric String

The examples showing the numeric value and string value is mentioned below -

let x = 10; //Numeric Value let y = "200" // String value

In some cases, Javascript will try to convert strings to numbers. for instance;

let num_1 = "200"; let num_2 = "20"; let result = num_1 / num_2; console.log(result) Output: 10

All the mathematical operations can work on the strings but no addition because when performing addition, the output will be string concatenation.

Javascript - Not a Number (NaN) and Infinity

The Nan is the keyword in javascript that identifies that the input value is not a legal number. for instance -

let number = 100 / "number"; console.log(number); //Output: Nan

The infinity is the value that Javascript will return when the calculation occurs outside the largest possible number. For instance -

let x = 2 / 0; consolel.log(x) //Output: infinity

Javascript Hexadecimal

Javascript represents the numbers with the base 8, by default. However, toString to output numbers from Base 2 to Base 36. for instance;

let num = 32; num.toString(8); num.toString(2);
//Output: 40 100000

Rounding

Rounding is an important concept that is useful when working with numbers. Rounding rounds off a number of certain conditions mentioned below -

  • Math.floor - It will return the lower value, 3.3 will become 3.
  • Math.ceil - It will return the higher value, 3.3 will become 4.
  • Math.round - It will round the number to the nearest integer, 3.3 will become 3, 3.7 will become 4.
  • Math.trunc - It will remove every value after decimal from the integer. 1.322 will become 1.
### Javascript Number Methods

Several Number Methods supported by Javascript are mentioned below -

  1. toExponential() - It will return the input number to the exponential notation.
  2. toString() - It will return the input number as a string.
  3. toPrecision() - It will return the input number with a specified length.
  4. valueOf() - It will return the input number as a number.
  5. toFixed() - It will return the input number with the number of decimals.

Javascript Number Properties

Several Number properties supported by the Javascript are mentioned below -

  • MAX_VALUE - It demonstrate the largest possible value in Javascript.
  • MIN_VALUE - It demonstrate the smallest possible value in Javascript.
  • MAX_SAFE_INTEGER - Maximum safe integer is (253 - 1).
  • MIN_SAFE_INTEGER - Minimum safe integer is (-253 - 1).
  • POSITIVE_INFINITY - Infinity
  • NEGATIVE_INFINITY - Negative Infinity.
  • EPSILON - It is the difference between the 1 and the smallest number.

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

Numbers and Strings Addition

Javascript - Not a Number (NaN) and Infinity

Javascript Hexadecimal

Rounding

Javascript Number Properties