Javascript Flashcards
How many types of number does Javascript have?
A single number type (represented by a 64bit float internally)
Is NaN equal to NaN?
No - it is equal to nothing else, therefore the only way to test for it is to use the function isNaN(number)
In Javascript, is ‘C’ + ‘a’ + ‘t’ === “Cat”?
Yes
Do strings have methods?
Yes - think of things like String1.toUppercase();
In Javascript do blocks create new scope?
No - so variables should be declared at the top of a function
What values typically evaluate to false?
false, null, undefined, ‘’, 0, NaN
What does the string ‘false’ evaluate to in Javascript
True
What is the ‘for in’ structure in Javascript?
A for loop.
for (var myvar in obj) { // Loop through }
What is a label in a break statement?
You can break to another location (labelled by label). i.e.
break myLabel;
What are the simple types in JavaScript?
numbers, strings, booleans, null and undefined
Are numbers, strings and booleans objects?
No - but they do have methods
Objects in JavaScript are…
Mutable keyed collections
Do you need to have a class in JavaScript to create an object?
No, objects in JavaScript are class free.
What is an object literal?
A pair of curly braces surrounding zero or more name/value pairs. i.e.
var empty_object = { };
var stooge = {
“first-name”: “jerome”,
“last-name”: “Howard”
};
What names must be enclosed in quotes in an object literal?
Names that don’t conform to normal variable naming conventions.
How do retrieve a variable from an object literal?
Using the [ ] operator
myObj[“name”]
How can you fill in a “default” value when assigning a value from an object literal?
var val = obj[“first-name”] || “(none)”;
(i.e. it will either provide the first-name element of the object or “(none)”
What happens when you try to retrieve values from “undefined”?
Throws a TypeError exception.
How do you prevent JavaScript throwing an exception when you accidentally try to access an element of an object that is undefined?
Use the && operator
flight.equipment // undefined
flight.equipment.model // throw “TypeError”
flight.equipment && flight.equipment.model // undefined
(I guess it fails on the left hand operand like other languages and doesn’t execute the right hand operand)
Objects in Javascript are passed by…
reference
What does setting “defer” on a script tag do?
It indicates to the browser that the script can start downloading immediately, but it wont’ be run until the page is loaded. It should only be used when using external
How would you specify the defer attribute in XHTML?
defer=”defer”
What is the async attribute used for?
To specify that a script can be run as soon as it is loaded (not wait for the page)
What is the potential issue with having multiple external scripts labeled as async?
They may run out of order, so there should be no dependancies between them.