The simplest implementation method for determining the font type in JavaScript, And the javascript font type

Source: Internet
Author: User

The simplest implementation method for determining the font type in JavaScript, And the javascript font type

This article mainly introduces the most concise implementation method for determining the entire word type in JavaScript. This article provides multiple methods for determining integers, and finally summarizes a shortest and simplest implementation method, for more information, see

 

 

We know that JavaScript provides the typeof operator, so it is most likely to use typeof to determine whether it is a number type.

 

Copy the Code as follows:
function isNumber(obj) {    return typeof obj === 'number'}

  

 

This function is no problem for integers and floating-point numbers, but it is uncomfortable to return true for NaN values. After all, no one will use NaN for arithmetic operations after isNumber is used to determine the result.

Try Object. prototype. toString.

 

Copy the Code as follows:
function isNumber(obj) {    return Object.prototype.toString.call(obj) === '[object Number]'}

 

 

Like typeof, true is returned for NaN, and the amount of code is large. This is not the desired result. The toString. call method is used to determine whether the js Array (Array) is feasible, and the number is insufficient.

Then, the NaN value is dealt with using the isNaN function.

 

Copy the Code as follows:
function isNumber(obj) {    return typeof obj === 'number' && !isNaN(obj)}

  

 

This time, if the input is not a number (NaN or can be converted to NaN value), false is returned.

 

Copy the Code as follows:
function isNumber(obj) {    return typeof obj === 'number' && !isNaN(obj)}isNumber(1)   // trueisNumber(1.2) // trueisNumber(NaN) // falseisNumber( parseInt('a') ) // false

  

 

Well, this isNumber is good, but there is another equivalent, which can be determined by isFinite.

 

Copy the Code as follows:
function isNumber(obj) {    return typeof obj === 'number' && isFinite(obj)    }

  

 

Up to now, the number of the shortest code is determined by the third isNaN function mentioned in this article. The following is a grand introduction of the world's shortest digital judgment Web Page code

 

Copy the Code as follows:
function isNumber(obj) {    return obj === +obj}

  

 

For integers, the floating point returns true, and the return value is false for NaN or NaN values that can be converted to NaN values.

Didn't you understand it, did you? Cool ~~ (Too many rows)

Yuan You said that this is not the world's shortest digital code, the parameter obj can be changed to a character. You are right.

Similar to the use of JS dynamic language features (internal automatic type conversion during operator operations.

 

Copy the Code as follows:
// Judge the string function isString (obj) {return obj = obj + ''} // judge the Boolean Type function isBoolean (obj) {return obj = !! Obj}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.