JavaScript was a doozy today. The program goes through a new subject of JavaScript…ok, this is hard to explain. The program will explain something, like ‘for loops’, and it will give one example and give you a problem to solve. Then when you are done with that and test the code, if you get it correct, the program will then go and talk about a new subject…say, ‘do while loops’. So you only get one example of how to do something when I feel like I need more instructions and more visual aid before going onto the next subject. So sometimes I feel like I’m still out of the loop, so to say, when it comes to different JavaScript features.
Alexis has a day off tomorrow but they get off work at 5am on on Saturday cause they work overnight on Friday. I know Lexi wanted to come home but I really feel that she should get some sleep. At least Sunday they go to work at 2pm for call time. I’m a little bummed cause I wanted to see her but sleep is important.
A friend of ours had a heart attack and is in the hospital. So Kel and I made chicken noodle soup yesterday at their house for them. It’s the only chicken noodle soup recipe that Tom will eat so I know it’s a good recipe. Since Tom doesn’t like chicken noodle soup. I like homemade chicken noodle soup but I won’t eat the can chicken noodle soup.
This weekend we are celebrating Tommy’s birthday and Father’s Day. I’m here for the food and cake! And to yes, celebrate these special days with Tommy. But don’t forget the food and cake! 🙂
JavaScript notes:
———————————-
When you declare a variable with the var keyword, it is declared globally, or locally if declared inside a function.
The let keyword behaves similarly, but with some extra features. When you declare a variable with the let keyword inside a block, statement, or expression, its scope is limited to that block, statement, or expression.
For example:
var numArray = []; for (var i = 0; i < 3; i++) { numArray.push(i); } console.log(numArray); console.log(i);
Here the console will display the values [0, 1, 2] and 3.
With the var keyword, i is declared globally. So when i++ is executed, it updates the global variable. This code is similar to the following:
var numArray = []; var i; for (i = 0; i < 3; i++) { numArray.push(i); } console.log(numArray); console.log(i);
Here the console will display the values [0, 1, 2] and 3.
This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the i variable. This is because the stored function will always refer to the value of the updated global i variable.
var printNumTwo; for (var i = 0; i < 3; i++) { if (i === 2) { printNumTwo = function() { return i; }; } } console.log(printNumTwo());
Here the console will display the value 3.
As you can see, printNumTwo() prints 3 and not 2. This is because the value assigned to i was updated and the printNumTwo() returns the global i and not the value i had when the function was created in the for loop. The let keyword does not follow this behavior:
let printNumTwo; for (let i = 0; i < 3; i++) { if (i === 2) { printNumTwo = function() { return i; }; } } console.log(printNumTwo()); console.log(i);
Here the console will display the value 2, and an error that i is not defined.
i is not defined because it was not declared in the global scope. It is only declared within the for loop statement. printNumTwo() returned the correct value because three different i variables with unique values (0, 1, and 2) were created by the let keyword within the loop statement.
The const declaration has many use cases in modern JavaScript.
Some developers prefer to assign all their variables using const by default, unless they know they will need to reassign the value. Only in that case, they use let.
However, it is important to understand that objects (including arrays and functions) assigned to a variable using const are still mutable. Using the const declaration only prevents reassignment of the variable identifier.
const s = [5, 6, 7]; s = [1, 2, 3]; s[2] = 45; console.log(s);
s = [1, 2, 3] will result in an error. After commenting out that line, the console.log will display the value [5, 6, 45].
As you can see, you can mutate the object [5, 6, 7] itself and the variable s will still point to the altered array [5, 6, 45]. Like all arrays, the array elements in s are mutable, but because const was used, you cannot use the variable identifier s to point to a different array using the assignment operator.