Introduction to JavaScript Number and Math Objects

Introduction to JavaScript Number and Math Objects

1. Number in JavaScript

In javascript , numeric types are primitive types. In addition to the decimal values ​​we know, we can also use 0x to represent hexadecimal integers, 0b to represent binary integers, and 0O to represent octal integers.

0xa //corresponds to decimal 10

0b101 //corresponds to decimal 5

0o22 //corresponds to decimal 18

In javascript , 0 can be used as a divisor and the return value is infinity. This is very different from other languages. For example, 0 cannot be used as a divisor in python .

1/0 // Infinity

Infinity represents infinite value

But the result of 0/0 is NaN . NaN is also a numeric type. It is a special value, which means it is not a "numeric" value.

When a non-numeric string is converted to a numeric type, NaN is also returned, for example, praseInt('a')

You can use Number.isNaN or the global function isNaN to determine whether a value is NaN

Number.isNaN(2) //false
Number.isNaN('a') // true
Number.isNan('2') //false


Numeric value is a primitive type, and there is also a corresponding wrapper object Number . Number class provides many methods, including isNaN mentioned above.

n = new Number(10)
n.valueOf() === 10 //true

The value corresponding to the object n defined by the wrapper class is exactly equal to the original type value 10.

2. Math Object in Javascript

Math object is a global object of Javascript , which provides many mathematical operation methods.

Math.max gets the maximum value:

let max = Math.max(1,2,3,4)
console.log(max) //4

Math.min gets the minimum value:

let min = Math.min(1,2,3,4)
console.log(min) //1

If you are looking for the maximum and minimum values ​​in an array, you can use the spread operator... to decompose the array into multiple parameter values.

Math.max(...[1,2,3,4]) //4

Math.ceil rounds up:

console.log(Math.ceil(2.3)) // 3

Math.floor rounds down:

console.log(Math.floor(2.8)) // 2

Math.round rounds to the nearest integer:

console.log(Math.round(2.5)) //3
console.log(Math.round(2.3)) //2


Math.random randomly generates a floating point number between [0,1), including 0 and excluding 1

console.log(Math.random())


Randomly generate an integer between 0 and 10

let num = Math.ceil(Math.random()*10)
console.log(num)


Math.pow power:

Math.pow(2, 10) //1024

This is the end of this article about the introduction of javascript Number and Math objects. For more relevant javascript Number and Math content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Javascript Basic Tutorial: Data Types (Number)
  • JavaScript Data Structure Number
  • JavaScript parseInt() and Number() difference case study
  • javascript converts other types to Number type
  • Detailed explanation of the 19-digit Number type precision loss problem in JS
  • Detailed explanation of JS numerical Number type
  • Number data type in JavaScript

<<:  How to implement element floating and clear floating with CSS

>>:  50 Beautiful FLASH Website Design Examples

Recommend

How to modify the forgotten password when installing MySQL on Mac

1. Install MySQL database on mac 1. Download MySQ...

Are the value ranges of int(3) and int(10) the same in mysql

Table of contents Question: answer: Reality: Know...

How to use nginx as a proxy cache

The purpose of using cache is to reduce the press...

Vue sample code for online preview of office files

I'm working on electronic archives recently, ...

MySQL 8.0 New Features: Hash Join

The MySQL development team officially released th...

About the problem of vertical centering of img and span in div

As shown below: XML/HTML CodeCopy content to clip...

VMware virtual machine three connection methods example analysis

NAT In this way, the virtual machine's networ...

How to use CSS attribute selectors to splice HTML DNA

CSS attribute selectors are amazing. They can hel...

Two methods to implement Mysql remote connection configuration

Two methods to implement Mysql remote connection ...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

SQL Practice Exercise: Online Mall Database Product Category Data Operation

Online shopping mall database-product category da...

Three ways to prevent MySQL from inserting duplicate data

Create a new table CREATE TABLE `person` ( `id` i...

Import CSS files using judgment conditions

Solution 1: Use conditional import in HTML docume...

About IE8 compatibility: Explanation of the X-UA-Compatible attribute

Problem description: Copy code The code is as fol...