• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if Variable is a String

Using JavaScript to Check if Variable is a String

June 10, 2022 Leave a Comment

One of the easiest ways we can use JavaScript to check if a variable is a string is by using the JavaScript typeof Operator in an if conditional statement.

if ( typeof someVariable == "string" ){
  //The variable IS a string
}

In the code above, someVariable is the variable that we want to check to see if it is a string. typeof someVariable will return “string” if the variable is a string, and therefore the conditional statement will be true.

We can put this code in a function to make checking if a variable is a string very simple.

Here is the function:

function isString(variable){
  if ( typeof variable == "string" ){
    return true;
  } else {
    return false;  
  }
};

Our function simply returns true if the variable you enter is a string, and false if not.

Now let’s show some examples of this function is use:

function isString(variable){
  if ( typeof variable == "string" ){
    return true;
  } else {
    return false;  
  }
};

var variable1 = "a string";
var variable2 = 1234;
var variable3 = "1234";
var variable4 = true;
var variable5 = "true";
var variable6 = "";

console.log(isString(variable1));
console.log(isString(variable2));
console.log(isString(variable3));
console.log(isString(variable4));
console.log(isString(variable5));
console.log(isString(variable6));

#Output:
true
false
true
false
true
true

Hopefully this article has been useful for you to learn how to use JavaScript to check if a variable is a string.

Other Articles You'll Also Like:

  • 1.  Set Hidden Field Value In JavaScript
  • 2.  Remove the Last Element From Array in JavaScript
  • 3.  How to Convert Degrees to Radians Using JavaScript
  • 4.  Using JavaScript to Get the Domain From URL
  • 5.  Using JavaScript to Check If String is a Number
  • 6.  JavaScript Check If Number is a Whole Number
  • 7.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 8.  Using JavaScript to Change Text of Element
  • 9.  Using JavaScript to Remove All Pipe Characters From String
  • 10.  Using JavaScript to Create a Unique Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy