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

Published March 14, 2021

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
  • 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 😃.

Share on: Facebook Twitter