Warning: session_start(): open(/tmp/sess_725cf09347f2533de6d2b1fda3752f48, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If A Character Is A Number Using JavaScript - LearnShareIT

How To Check If A Character Is A Number Using JavaScript

Check if a Character is a Number using JavaScript

We can use comparison operators to check if a character is a number using JavaScript. Follow this article to learn how to do it with the explanation and examples below. Let’s go into detail now.

Check If A Character Is A Number Using JavaScript

These are some methods to check if a character is a number using JavaScript.

Use comparison operators

You can check if a character is a number by using comparison operators. You have to compare the character with the character ‘0’ and ‘9’, not the number 0 or 9. The character satisfies the condition is not less than ‘0’ and is not greater than ‘9’.

Look at the example below to learn more about this method.

function isNumber(char){

    // if the size of the char is > 1, it is a String, not a Character.
    if (char.length > 1) return false

    if (char >= '0' && char <= '9') {
        return true
    }
    return false

}

let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))

Output 

false
true
false
false

Use isNaN() method

The isNaN() method returns true if the specified value is a Number. Otherwise, it returns false.

Syntax:

isNaN(value)

Parameters:

  • value: The specified value.

Return Value: true or false.

Look at the example below:

function isNumber(char){

    // if the size of the char is > 1, it is a String, not a Character.
    if (char.length > 1) return false

    if (!isNaN(char)) {
        return true
    }
    return false

}

let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))

Output 

false
true
false
false

Use the parseInt() method

The parseInt() method returns an integer if the specified value is an integer. Otherwise, it returns NaN. So you can use isNaN() method with the parseInt() method inside to check if a character is a number or not.

Take a look in the example below:

function isNumber(char){

    // if the size of the char is > 1, it is a String, not a Character.
    if (char.length > 1) return false

    if (!isNaN(parseInt(char))) {
        return true
    }
    return false

}

let char1 = 'a'
let char2 = '1'
let char3 = '11'
let char4 = '1a'
console.log(isNumber(char1))
console.log(isNumber(char2))
console.log(isNumber(char3))
console.log(isNumber(char4))

Output 

false
true
false
false

Like the parseInt() method, you can use the number object or the ‘+’ operator and the isNaN() method to check if a character is a number or not.

You can learn the syntax of using the number object or the ‘+’ operator here.

Summary

To check if a character is a number using JavaScript, you can use the comparison operators, isNaN() method or parseInt() method. Choose the method that is suitable for you. We hope this guide is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *