Too unforgiving tests in JS: Use typeof to Check the Type of a Variable
See original GitHub issueThe test regexes don’t allow for spaces before the parentheses, which is unnecessarily strict.
Affected page
Your code
console.log (typeof seven);
console.log (typeof three);
Expected behavior
Same as:
console.log(typeof seven);
console.log(typeof three);
Issue Analytics
- State:
- Created a year ago
- Comments:13 (10 by maintainers)
Top Results From Across the Web
JavaScript Type Checking – How to Check Type in JS with ...
To validate variables by checking their types in JavaScript, you can use the typeof operator. Type checking in JavaScript is not ...
Read more >what is the shortest way to check for typeof any type?
Checking types in Javascript is a big mess. typeof ... Yet another way to test for type information is by using the Function.prototype....
Read more >typeof - JavaScript - MDN Web Docs - Mozilla
The typeof operator returns a string indicating the type of the operand's value.
Read more >How to check the type of a variable or object in JavaScript
In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically...
Read more >JavaScript Coding Standards
Type Checks · Global Variables: typeof variable === 'undefined' · Local Variables: variable === undefined · Properties: object.prop === undefined · Any of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Neither syntax is incorrect, one is just less common. There is already wide support for being lax in tests like these. Most recently the discussion was in the previously linked #45575, where the core team weighed in as well. So the main thing that should be done in this bug is just to fix the tests. Other bugs should have their own issues.
Some people do it but it is really uncommon. Making the code readable to other people is part of getting hired. This just isn’t a thing that you see in production code. JS has a bunch of these little things that are technically possible but never used. There is a huge difference between bikeshedding on indentation or semicolons and going out of our way to support baffling, non-standard syntax that nobody uses in professional code.
We’re made notes about this sort of thing in the past so learners don’t get in the habit of writing technically correct but bizzaro code:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes
And all of our examples of using functions follow the basic convention in JS of not jamming extra spaces in there, from the beginning:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions
I don’t see why we’d be obligated to support every single non-standard thing that a learner can imagine.