I need to check if the variable is a string, what should I do?

Asked 2 years ago, Updated 2 years ago, 83 views

So far, we've used the isNaN function to check whether the variable is a string or an object, but it doesn't seem to work properly I wonder if I did it the right way.

if(isNaN(element))
    element = document.querySelector(element);

I think there's something wrong, but I don't know what's right.

javascript string object

2022-09-22 08:57

1 Answers

Try using the type of operator.

var booleanValue = true; 
var numericalValue = 354;
var stringValue = "This is a String";
alert(typeof booleanValue) // "boolean"
alert(typeof numericalValue) // "number"
alert(typeof stringValue) // "string"


2022-09-22 08:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.