DEV Community

Cover image for How to check if a variable is a string in JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to check if a variable is a string in JavaScript?

Originally posted here!

To check if a variable is a string, you can use the typeof operator followed by the variable or expression in JavaScript.

// Check if variable is a string
const str = "Hello World!";

typeof str; // string
Enter fullscreen mode Exit fullscreen mode
  • The operator will return the type of the variable as a string, in our case, it will be a string.

See the above code live in JSBin.

Feel free to share if you found this useful 😃.


Top comments (0)