00:24 You’ve seen how sloppy I’ve been about it in other lessons. There are some educators, though. Consider the following code.
00:33 This will result in a parsing error. That’s because the parser doesn’t pay any attention to newlines. It sees that code snippet as this. You can’t name a function with a number, so you get an error. To get this to work as expected, you want to be explicit about your semi-colon.
This quirk can be even trickier. You could conceive of a weird edge case where instead of adding
3, you are adding a variable. The same thing would happen, but you wouldn’t get an obvious parsing error. You’d get a
RuntimeError when it tried to call your variable as a function.
for loop structure.
The three parts of it indicate an initializer that is run before the loop, a comparator that if
True allows the next iteration of the loop to run, and the third section, run before entering the next iteration of a loop, typically used to increment your counter.
You can leave the initializer, condition, and incremental portions of the
while loop and a
This is like a
while loop, but the condition check is done after the first iteration. This can be useful if you want to do something once for sure, and then possibly repeat it a few more times.
break keywords to skip to the top of an iteration or leave the loop altogether, respectively.
Previously, I spoke about my nemesis, the
in keyword. You can use this in a
for loop to iterate over the key names of an object.
As I point out before, you can’t use this with an array. This also has the confounding feature that it includes things attached to the prototype. So if you’ve got a richer object, rather than just a vanilla dictionary-like thing, you may get more than you expect. The attributes returned by the
in keyword have a method called
hasOwnProperty(), which will return
True if the attribute belongs to the object instance. To safely use
in together, you should check that the attribute belongs to the instance of the object.
To get around the pesky “arrays don’t work with
of keyword. For arrays, this behaves like the
for iterator in Python.
.forEach(). This takes a function with a single parameter.
03:12 The function is called once for each item in the array, passing the parameter in. I’ve shown it here with an arrow operator, but it works with the regular anonymous function as well.
When using a function to build a constructor object, instead of the ES6 class concept, you can run into a problem if you forget the
new keyword. When I build
bob, it works.
When I build
joe, I get back nothing. It’s
undefined. If you forget the
new keyword, you won’t get an object back.
03:46 There’s some clever tricks you can use to check whether this is happening inside the constructor function to prevent this problem, but I prefer to just stick with ES6 classes. You’ll get an error if you make this mistake with a class.
In a previous lesson, I touched on the fact that everything is a global declaration by default. This can cause unexpected behaviors if you forget to use the
You may recall that the
var keyword defines context-level scoping. If it’s in the global space, it is global. If it is in a function, it is at the function level.
It is important to note that it doesn’t matter where in the function it is declared. It is visible to the whole function. Declaring a variable inside code blocks like
if statements or loops doesn’t hide the variable from the function.
04:32 This can cause some unexpected behaviors. To demonstrate, here’s a little puzzle. Take a second and read the code. Write down what you think the output will be.
How’d you do? Don’t worry if you didn’t get it. Something a bit strange is going on here. It is called variable hoisting. The declaration of
var x in the function causes the namespace override for the whole function even before the line where it is declared.
05:02 What is happening under the covers is closer to the following code.
This means you can call a function before it appears in the code. Be careful, though. If you use
var to define a reference to a function expression, it will get hoisted and the variable definition will exist before the function expression assignment, and you’re back to having a problem.
These are all avoidable problems, simply by using the modern
const declaration methods instead.
05:42 And the fun just keeps on coming! Next up, quirkier quirks.
Become a Member to join the conversation.