Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand javascript Number and Math objects

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand javascript Number and Math objects". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to understand javascript Number and Math objects.

1. Number in javascript

In javascript, the numeric type belongs to the primitive type. In addition to the ordinary values we know in decimal, we can also use 0x to represent hexadecimal integers, 0b for binary integers, and 0O for octal integers

0xa / / Decimal 100b101 / / Decimal 50o22 / / Decimal 18

In javascript,0, it 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 stands for infinity

But the result is NaN, and NaN is also a numeric type, which is a special number, indicating that it is not a "numeric" value.

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

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

Number.isNaN (2) / / falseNumber.isNaN ('a') / / trueNumber.isNan ('2') / / false

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

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

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

2. Math object in Javascript

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

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 and 2) 3) console.log (min) / / 1

If you are looking for the maximum and minimum values in an array, you can use the extension operator. Solve the array into multiple parameter values

Math.max () / / 4

Math.ceil rounding up:

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

Math.floor rounded down:

Console.log (Math.floor) / / 2

Math.round is rounded:

Console.log (Math.round) / / 3console.log (Math.round) / / 2

Math.random randomly generates a floating-point number between [0Pol 1), including 0 but not 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 Thank you for your reading, the above is the content of "how to understand javascript Number and Math objects". After the study of this article, I believe you have a deeper understanding of how to understand javascript Number and Math objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report