diving into JavaScript (coding diary #6)

Michael Scoggins |
7/19/2020

1. Describe one thing youâre learning in class today.
loops! a for loop is pretty awesome. start with an iterator, i = 0
, create a condition that evaluates to true until you want the loop to stop, i < 10, and then set an incrementer or decrementor, i++
or i--
, for example, and then run a set of instructions through it by setting your variables to i
. sound confusing? IT IS! but itâs still really cool once you learn how to use them, as it would be impossible to do the set of instructions in a human lifetime, given a task such as iterating through something a million times or whatever (or even something really hard 1,000 times).
for clarity, a for loop could look like:
for (let i = 0; i < 1000; i++) {
console.log(i);
}
and your console would count to 1,000 for you, because who got time for dat?
2. What is "use strict";
? What are the advantages and disadvantages to using it?
âuse strict
â is string you write at the top of your JavaScript, or your <script>
tag, or even inside a function, that forces the programmer to write cleaner JavaScript (by throwing actual errors when bad practices are used). examples include using undeclared variables and objects, duplicating parameter names, deleting undeleteable properties, using the name âargumentsâ for a variable, and other quirks of the language that could break your code further down the line if unaddressed.
3. Explain function hoisting in JavaScript.
hoisting is where declarations get âhoistedâ to the top of the current scope, so that variables can be âusedâ even before they are âdeclared.â
4. Explain the importance of standards and standards bodies like ECMA.
these standards are indispensable to the smooth implementation of JavaScript features while still allowing users or browsers who havenât updated to the cutting-edge standards to view âlegacyâ code. and without a universal standard by which all JavaScript engines abide, then programmers would have an *impossible* time writing code that all users and viewers in the world could access and benefit from.
5. What actions have you personally taken on recent projects to increase maintainability of your code?
camelCase, very specific class and id names which utilize lowercase-and-dashes, proper indentation, using spaces between operators, returning on lines in order to better group declarations or the like, and implementing useful comments.
6. Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
because you donât want variables being read outside of their local scope. itâs a very bad idea to declare global variables inside of (well, anything) a function, for example. and code readability takes a hit as well, when same-named variables are used in multiple contexts (and is a generally lazy practice).