Week-4 Flashcards
Javascript is always executed sequentially
True
HTML:
Is this HTML valid?
No, it’s missing a closing ‘script’ tag. It must always be there.
Identify valid Javascript code snippet(s)
a) var x = "Hello world!"; Regular variable declaration. b) var x = function () { console.log("Hello World"); return; }; It's valid. The 'return' statement here doesn't do much as the function will return anyway without it. c) var x = function () { console.log("Hello World"); return; }; x(); Valid. The last line simply executes/invokes the function. d) var x = function () { console.log("Hello World"); return; }; x = "hello, I am a new value!"; Valid. Javascript has dynamic typing, so 'x' can be a function and then can change to be a regular string.
An object in Javascript is simply a collection of name/value pairs.
True
In Javascript, a primitive type can store only 1 name/value pair at a time.
False as Primitive values are NOT name/value pairs. They are single values.
Strict equality operator (===) differs from regular equality operator (==) in that it checks if both values on its right and left are of the same type first. If they are not, it doesn’t try to coerce them to be the same value and just returns false.
True
JS:
var x = 10;
if ( (null) || (console.log(“Hello”)) || x > 5 ) {
console.log(“Hello”);
}
How many times will the word ‘Hello’ be printed to the console?
2
Since we are using an OR operator, the ‘if’ statement will stop evaluating conditions after it encounters the very first clause that evaluates to true.
null is false, so we continue
console.log(“Hello”), prints “Hello” to the console and returns nothing, so it’s ‘undefined’ and ‘undefined’ is false, so we continue
x > 5 is true, so we execute the body of the ‘if’ statement and print another “Hello”.
So, the answer is that “Hello” will be printed to the console 2 times.
Defining a variable as an Object Literal accomplishes pretty much the same thing as defining a variable equal to ‘new Object()’. However, it’s faster and easier to type up an object literal.
True
What is the output for the following Javascript code: function makeMultiplier(multiplier) { var myFunFunc = function (x) { return multiplier * x; };
return myFunFunc;
}
var operation = makeMultiplier(10); console.log(operation(10));
100
Javascript doesn’t allow anything to be passed by reference
False
Objects are passed by reference.
Output of: var x = 5; var y = x; x = 10; console.log(y);
5
as Primitives are copied by value, so ‘y’ is not connected to ‘x’ in any way.
Output of JS: function Dog(name) { this.name = name; }
Dog.prototype.bark = function () {
console.log(this.name + “ likes barking! Bark!”);
}
var max = new Dog("Max", "Buddy"); max.bark();
Max likes barking! Bark!
as The second argument is not used in the function but it’s perfectly legal to pass it in.
Output of JS:
var counter = 0;
var myArray = [“Yaakov”, 2, {handle: “yaakovchaikin”}];
for (var i = 0; i < myArray.length; i++) {
counter++;
}
console.log(counter);
3
Immediately Invoked Function Expressions are usually used to place code into its own execution context not to conflict with the global scope.
True
Code: 1 (function(window) { 2 3 var obj = {}; 4 5 obj.dreamOn = function () { 6 console.log("I want to see the global scope! Let me out!"); 7 }; 8 9 window.doer = obj; 10 11 }); 12 13 doer.dreamOn(); What (if anything) is missing from this code in order to get it to output the message of the "dreamOn" method?
The attempted IIFE is not being invoked and passed in the window object. It’s missing ‘(window)’ Line 11 right before the semicolon.