3 ms·
Quick JavaScript Reference on GitHub
- Rockslide 11y agoIt's funny to read something like var isString = typeof string === "string" ? true : false; in a file called "bestPractices.js". I'm totally aware that this example is about the ternary operator and not about boolean logic, but if you want to teach best practices, then your examples should be the best practice in every respect. Testing a boolean expression and assigning "true" or "false" to a variable accordingly is one of my personal indicators about the advance of someones mental programming model ;) So why not instead: var textColor = hasSpellingMistakes(text) ? "#F00" : "#000"
- magic_beans 11y agoHow would you re-write: var isString = typeof string === "string" ? true : false; ?? Just curious, as I can't tell what's wrong here...
- Rockslide 11y agoNothing is technically wrong. But I would write it as var isString = typeof string === "string";
- stevenspasbo 11y agoAs rockslide said, it's not incorrect, it's just overly verbose.
- r_z_ 11y agoYour point would be valid if the object of the example wasn't to show, literally, how the ternary operator works. But thanks for the suggestion!