You were close:
if (typeof a_string === 'string') {
// this is a string
}
On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that way, ever.
You were close:
if (typeof a_string === 'string') {
// this is a string
}
On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that way, ever.
The typeof operator isn't an infix (so the LHS of your example doesn't make sense).
You need to use it like so...
if (typeof a_string == 'string') {
// This is a string.
}
Remember, typeof is an operator, not a function. Despite this, you will see typeof(var) being used a lot in the wild. This makes as much sense as var a = 4 + (1).
Also, you may as well use == (equality comparison operator) since both operands are Strings (typeof always returns a String), JavaScript is defined to perform the same steps had I used === (strict comparison operator).
As Box9 mentions, this won't detect a instantiated String object.
You can detect for that with....
var isString = str instanceof String;
jsFiddle.
...or...
var isString = str.constructor == String;
jsFiddle.
But this won't work in a multi window environment (think iframes).
You can get around this with...
var isString = Object.prototype.toString.call(str) == '[object String]';
jsFiddle.
But again, (as Box9 mentions), you are better off just using the literal String format, e.g. var str = 'I am a string';.
Further Reading.
When using a typeof operator on a string why do i have to spell out string with quotations? Why can't I use an empty string?
A question about typeof operator in JS
How do you check if some type matches your custom type?
[AskJS] typeof null = string???
That's what you get for using var.
name is a property of the global window (or globalThis) object that is a string.
Variables defined with var at the top level of a script (not within a function) get attached to the global object, window, whether they already exist or not.
In this case, whatever you assign to it also gets stringified because that's what the browser/spec requires window.name to be.
var name = null at the top level of the script is equivalent to window.name = null. typeof name is then the same as typeof window.name.
let name = null; console.log(typeof name); // 'object'
The learning here is: use const by default, let when you need to reassign the value. Forget var exists.
Videos
In meal setter why must I spell out string when using typeof operator instead of having an empty string? And why must i wrap a number in quotation marks, isnt a number in quotation marks change it from a number to a string? Is this just an exemption for setters?
const menu={_meal: ' ', _price: 0,
set meal(mealToCheck){if(typeof mealToCheck==='string'){return this._meal=mealToCheck}},
set price(priceToCheck){if(typeof priceToCheck==='number'){return this._price=priceToCheck}},
get todaysSpecial(){if(this._meal && this._price){return 'correct'} else {return 'wrong'}}
}
menu.meal='pop'
menu.price=9
console.log(menu.todaysSpecial)