How to useinstanceof operator in Javascript
instanceof operator: It checks whether the LHS (left-hand side) object is an object of the RHS (right-hand side) class or not. If the object is of that particular class, then it returns true otherwise false.
Example: The first myString is not an instance of String, as it’s a literal. The second myStringObj is an instance of String as it’s an object.
Javascript
//checking for string const myString = "Hello, Geeks" ; console.log(myString instanceof String); // checking for mystringObj is string or not const myStringObj = new String( "Hello, w3wiki" ); console.log(myStringObj instanceof String); |
Output
false true
How to test a string as a literal and as an object in JavaScript ?
In this article, we learn how to test a string as a literal and as an object using JavaScript.