3 ms·
Bullshit. Using foo = bar || baz is perfectly valid when used judiciously. foo = bar || baz is perfect when parsing inputs where unset is passed as "" or 0. To
by rdancer 11y ago
Bullshit. Using foo = bar || baz is perfectly valid when used judiciously.
foo = bar || baz is perfect when parsing inputs where unset is passed as "" or 0. To no great surprise, HTML forms are one such beast.
The Boolean operators are simply a shorthand for if (!foo) foo = bar -- when you mean if (foo === undefined) foo = bar; then simply write that instead. At least omit the unnecessary braces and white space.
Verbose code is not necessarily more readable. You may be able to code your way around a reader not comprehending JS rules regarding Boolean coercion, but those rules are based on real world inputs which often do not have a special 'undefined' value.
- tracker1 11y agoI came in here to write pretty much the same thing... the only issue for me tends to be when 0 is allowed. As an interview problem, I will ask someone to write in JavaScript a method that takes a single parameter, that will always return a number or null.. if it's a string that represents a number, convert it, otherwise it should be null. 0 always trips people up in this, or other means of coercion. Other than that one case, I find the shortcut of || to be pretty much invaluable. This is an example of something you should just learn how it works, rather then assume it's stupid or wrong because you don't know how the environment works. You should have seen how many people went up in arms at LINQ and even Lambda functions in C#. I've seen similar comments about lambda/fat-arrow functions coming into JS.
- acjohnson55 11y agoIs that a great interview question? It strikes me as the sort of thing where a positive doesn't tell you all that much additional about someone's engineering ability, nor does a negative.
- tracker1 11y agoIt isn't the only thing I judge on, but when you see a stack of resumes where people put their JavaScript knowledge as "expert" ... I expect it to be a relatively easy question. It comes down to a level of honesty, and when someone says they are an expert with a language, they better know that language pretty well. Like "what are the falsy values in JavaScript" (I usually let people get away with 3+ of them) In the browser a very primary purpose for JavaScript is input validation... this includes strings... being able to reuse a method for internal validation is useful as well. Knowing how to do something useful in a language is a perfectly fine interview question. It isn't like I'm asking people to write a bubble-sort, or how an IEEE 754 number is structured and how that relates to JS... Or how to determine the number of marbles in a given jar... or any other of other questions I've been asked in an interview. I'm asking someone to validate a simple input in one of the most widely used programming languages on the planet. Sorry to go on a rant.. but hiring is hard, and when you are looking at someone why says they have a lot of experience with the skills that you are looking for (full-stack javascript/node)... expectations shouldn't be too low. I'm more than happy to talk to people with other backgrounds and skills... I've hired plenty of newer/eager learners who don't have a lot of experience, but demonstrated the ability to work through a problem. In my mind effort/ambition/trying-stuff means more than specific experience.