In JavaScript, I can declare a string in the following ways;
var a = "Hello World"; var b = new String("Hello World");
but a is not an instance of String...
console.log(a instanceof String); //false; console.log(b instanceof String); //true;
So how do you find the type or "instanceof
" a string literal?
Can JavaScript be forced to create a new String()
for every string literal?
The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. Parameters: objectName: States the name of Object.
In that case, the template literal is passed to your tag function, where you can then perform whatever operations you want on the different parts of the template literal. To escape a backtick in a template literal, put a backslash ( \ ) before the backtick.
Template literals provide an easy way to interpolate variables and expressions into strings. The method is called string interpolation.
use typeof "foo" === "string"
instead of instanceof.
Use typeof
instead and just compare the resulting string. See docs for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With