1. Write a JavaScript program that declares a function but calls it before it is
ID: 3821299 • Letter: 1
Question
1. Write a JavaScript program that declares a function but calls it before it is declared. Because of function hoisting this will work in JavaScript. Go prove it!
Also write a function which is assigned to a variable. Call it before it is assigned and prove that this does not work.
2. The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties.
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual.
To find out whether to compare two things by identity (use the === operator for that) or by looking at their properties, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: by a historical accident, typeof null also produces "object".
Explanation / Answer
1.
Write a JavaScript program that declares a function but calls it before it is declared. Because of function hoisting this will work in JavaScript. Go prove it!
console.log(sayHello());
function sayHello() {
return "Hello. Thanks Hosting for saving my program.";
}
Also write a function which is assigned to a variable. Call it before it is assigned and prove that this does not work.
console.log(sayHello1());
sayHello1 = function () {
return "Hello. Thanks Hosting for saving my program.";
}
This will fail with Uncaught ReferenceError: sayHello1 is not defined
2. The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties.
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual.
To find out whether to compare two things by identity (use the === operator for that) or by looking at their properties, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: by a historical accident, typeof null also produces "object".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.