How to usetypeof operator in Javascript
The typeof operator in JavaScript returns a string that identifies the data type of an expression. It is used to determine the data type (returns a string) of its operands. Operands can either be literals or data structures, such as variables, functions, or objects. An operator returns the type of data. The result of typeof can be an object, a boolean, a function, a number, a string, or an undefined value.
Example: In this example, we are going to check if the string is an object or a literal using the if-else condition.
Javascript
const myString = "Hello, Geeks" ; console.log( typeof myString); const myStringObj = new String( "Hello, w3wiki" ); console.log( typeof myStringObj); |
Output
string object
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.