Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check if a string is a string in javascript, use the typeof keyword it will return "string" if the value is a string.

Today, I’m going to show you How do I check if a string is a string in javascript, as mentioned above, I’m going to create a function to check value is a string or not.

Let’s start today’s tutorial on how do you check if a string is a string in javascript.

Javascript check string is a string

Here, we will do

  1. Create a function isString() and validate the param is a string using typeof.
  2. If it’s a string return true else false.
function isString(param) {
    // check param datatype
    if(typeof param === "string"){
        return true;
    } else {
        return false;
    }
}

// true
console.log(isString("hello"))

// false
console.log(isString(99032))

// false
console.log(isString(true))

Output

  • If you want to validate your string containing alphabets only read the below tutorial.

javascript regex for alphabets only

  • If you want to validate your string containing alphabets and numbers only read the below tutorial.

javascript regex for alphanumeric

I hope it helps you, All the best đź‘Ť.