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.