Errors Flashcards
Errors:
What is the value of “d” in this statement?
var d = 7 [1, 2, 3].forEach(function ()... )
undefined
If a semicolon is not included at the end of a line it will be combined with the subsequent line if the subsequent line begins with (, [, +, - or /
After the statement is parsed it will be
var d = 7[1, 2, 3].forEach(function ()… )
Beware!
Errors:
To combine “strict” and “non-strict” file safely, you can wrap the code in what type of functions?
Immediately invoked function expressions (IIFE)
(function( ) { /* code */ }) ( );
Errors:
When joining multiple JavaScript files how can you defend against the accidental concatenation of functions?
By prefixing the file with a semicolon.
;function () {….}
Errors:
What tools help diagnose possible coding error and bad practices that can lead to faulty code?
Lint tools
It is a good practice to use a program like lint or hint to check for errors that might be hidden in your code.
Errors:
If the JavaScript engine is unable to look up the value of a variable (typically the right hand side of an equation) what error will it throw?
ReferenceError
In strict mode this will also happen for variables on the left hand side of the equation because strict mode will not automatically generate missing variable declaration in the global space.
Errors:
If a variable is found but you try to do something illegal with it (like reference a property on undefined) what error is thrown?
TypeError
TypeError implies the scope was found while ReferenceError implies the scope was not found.
Errors:
Will the code
var CapVar = 5; capVar += 8;
throw an error?
Only in strict mode.
If you are not in strict mode misspelled variables will be declared by JavaScript as a new global variable an the execution will continue.
In strict mode using undeclared variables will throw an error.