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.

Answer from David Tang on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › typeof
typeof - JavaScript - MDN Web Docs - Mozilla
It's used in Object.prototype.toString(). const tag = value[Symbol.toStringTag]; if (typeof tag === "string") { return tag; } // If it's a function whose source code starts with the "class" keyword if ( baseType === "function" && Function.prototype.toString.call(value).startsWith("class") ) { return "class"; } // The name of the constructor; for example `Array`, `GeneratorFunction`, // `Number`, `String`, `Boolean` or `MyCustomClass` const className = value.constructor.name; if (typeof className === "string" && className !== "") { return className; } // At this point there's no robust way to get the type of value, // so we use the base implementation. return baseType; } For checking potentially non-existent variables that would otherwise throw a ReferenceError, use typeof nonExistentVar === "undefined" because this behavior cannot be mimicked with custom code.
Discussions

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?
Because typeof someVar returns a string and you’re checking a string against that string. You can use instanceof if you want to compare a variable directly against another Class. More on reddit.com
🌐 r/learnjavascript
3
1
April 14, 2024
A question about typeof operator in JS
I’m curious to sort out a little curiosity about the typeof operator in JS. The typeof operator defines the type of a value/variable. My question is: why so? Isn’t that obvious? “33” is obviously a string, so “33” === 33 results unequal, therefore false. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
June 22, 2020
How do you check if some type matches your custom type?
Obviously zod's enum method has to get a mention here More on reddit.com
🌐 r/typescript
5
1
January 17, 2025
[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.

More on reddit.com
🌐 r/javascript
19
28
June 25, 2024
🌐
W3Schools
w3schools.com › js › js_typeof.asp
JavaScript typeof
The typeof operator returns the data type of a JavaScript variable. In JavaScript, a primitive value is a single value with no properties or methods. ... The typeof operator returns the type of a variable or an expression. typeof "John" // Returns ...
🌐
Tabnine
tabnine.com › home › how to use the typeof operator in javascript
How to Use the typeof operator in JavaScript - Tabnine
July 25, 2024 - The typeof operator accepts an operand and returns the type of the provided operand as a string. The following sample code shows the typeof operator in use: const str = 'Im a string'; console.log(typeof str); // Expected output: string In the ...
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › typeof-types.html
TypeScript: Documentation - Typeof Type Operator
JavaScript already has a typeof operator you can use in an expression context: ts · // Prints "string" console. log(typeof "Hello world");Try · TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable or property: ts ·
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
The typeof operator in JavaScript checks and returns a string of the data type of a variable. It can return up to nine possible data types for JavaScript variables, including string, number, array and boolean.
🌐
Medium
piyush-dubey.medium.com › advanced-guide-to-javascript-typeof-operator-d88eb12f0ff0
Advanced Guide to JavaScript typeof Operator | by Piyush | Medium
September 22, 2024 - console.log(typeof 42); // "number" console.log(typeof 'Hello World'); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof {}); // "object" console.log(typeof function(){}); // "function" console.log(typeof Symbol()); // "symbol"
Find elsewhere
🌐
Medium
medium.com › theleanprogrammer › javascript-typeof-228ed8e296d5
The Javascript “typeof” Operator. Returning Strings Indicating The Type… | by Harsha | TheLeanProgrammer | Medium
June 1, 2021 - typeof ‘’ === ‘string’; typeof ‘bla’ === ‘string’; typeof `template literal` === ‘string’; typeof ‘1’ === ‘string’; // note that a number within a string is still typeof string typeof String(1) === ‘string’; // String converts anything into a string, safer than toString typeof (typeof 1) === ‘string’; // typeof always returns a string
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-typeof-operator
JavaScript typeof Operator - GeeksforGeeks
December 13, 2024 - console.log(typeof "Hello"); console.log(typeof 42); console.log(typeof true); console.log(typeof {}); console.log(typeof undefined); ... Always outputs the data type as a string.
🌐
Reddit
reddit.com › r/learnjavascript › 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?
r/learnjavascript on Reddit: 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?
April 14, 2024 -

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)

🌐
W3Schools
w3schools.com › js › js_datatypes.asp
JavaScript Data Types
typeof "" // Returns "string" typeof "John" // Returns "string" typeof "John Doe" // Returns "string" Try it Yourself »
🌐
freeCodeCamp
freecodecamp.org › news › javascript-typeof-how-to-check-the-type-of-a-variable-or-object-in-js
JavaScript TypeOf – How to Check the Type of a Variable or Object in JS
November 9, 2020 - In the above example, the expression typeof 007 evaluates to the type number and returns the string 'number'. typeof('number') then results in 'string'.
🌐
Zipy
zipy.ai › blog › check-if-a-variable-is-a-string-in-javascript
check if a variable is a string in javascript
April 12, 2024 - Pros: Works for both string primitives and String objects. Cons: Might throw an error if the variable is undefined or null. For a more robust solution, especially in complex applications, you might consider implementing a custom function that encompasses these checks: function isString(value) { return typeof value === 'string' || value instanceof String; } console.log(isString("Yes, I'm a string!")); // true console.log(isString(new String("String object"))); // true console.log(isString(123)); // false
🌐
StaticMania
staticmania.com › blog › using-typeof-in-javascript-to-check-data-types
Using typeof in JavaScript to Check Data Types - StaticMania
December 22, 2024 - The typeof operator returns a string that indicates the current type of a variable.
🌐
Sentry
sentry.io › sentry answers › javascript › check if a variable is a string in javascript
Check if a variable is a string in JavaScript | Sentry
July 15, 2023 - // Function to test if variable is a string function isString(variable) { return typeof variable === "string"; } // Variables of different types let myBool = false; let myNum = 12; let myArray = [1, 2, 3] let myObject = { a: 1, b: 2, c: 3} let myString = "Hello world!"
🌐
Unibo
lia.disi.unibo.it › materiale › JS › developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › typeof.html
typeof - JavaScript | MDN
April 11, 2015 - // Numbers typeof 37 === 'number'; typeof 3.14 === 'number'; typeof Math.LN2 === 'number'; typeof Infinity === 'number'; typeof NaN === 'number'; // Despite being "Not-A-Number" typeof Number(1) === 'number'; // but never use this form! // Strings typeof "" === 'string'; typeof "bla" === 'string'; typeof (typeof 1) === 'string'; // typeof always return a string typeof String("abc") === 'string'; // but never use this form!
🌐
freeCodeCamp
forum.freecodecamp.org › t › a-question-about-typeof-operator-in-js › 406846
A question about typeof operator in JS - The freeCodeCamp Forum
June 22, 2020 - I’m curious to sort out a little curiosity about the typeof operator in JS. The typeof operator defines the type of a value/variable. My question is: why so? Isn’t that obvious?
🌐
W3Resource
w3resource.com › javascript › operators › typeof.php
JavaScript : typeof operator - w3resource
September 30, 2023 - > typeof {a:1} "object" > typeof new Date() "object" > typeof null "object" > typeof /a-z/ "object" > typeof Math "object" > typeof JSON "object" Examples of typeof operator: undefined · Console · > typeof undefined "undefined" > typeof abc "undefined" More examples on typeof operator · typeof(4+7); //returns number typeof("4"+"7"); //returns string typeof(4*"7"); //returns number typeof(4+"7"); //returns string ·
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_typeof_operator.htm
JavaScript - typeof Operator
The typeof operator in JavaScript is a unary operator used to get the data type of a particular variable. It is placed before its single operand, which can be of any type. Its returns a string value indicating the data type of its operand.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-type-checking-how-to-check-type-in-js-with-typeof
JavaScript Type Checking – How to Check Type in JS with typeof()
November 7, 2024 - The typeof operator will return the type as a string, meaning “number”, “string”, "boolean”, and lots more.