question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Too unforgiving tests in JS: Use typeof to Check the Type of a Variable

See original GitHub issue

The test regexes don’t allow for spaces before the parentheses, which is unnecessarily strict.

Affected page

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/debugging/use-typeof-to-check-the-type-of-a-variable

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:closed
  • Created a year ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
lynxlynxlynxcommented, Apr 12, 2022

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.

1reaction
jeremyltcommented, Mar 30, 2022

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

Note: There shouldn’t be any spaces between the array name and the square brackets, like array [0]. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.

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

You can call or invoke this function by using its name followed by parentheses, like this: functionName();

I don’t see why we’d be obligated to support every single non-standard thing that a learner can imagine.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found