Operators, data types, some syntax Flashcards
How can you include an external javascript file?
/script src=’myfile.js’/
What’s better, putting scripts just before the closing body tag or in the head with ‘defer’ attribute?
Sort of a wash, but old browsers don’t understand defer so before probably better
ECMAScript has how many data types?
5 simple: undefined, null, number, boolean, string;
and 1 complex: object
how do you find the data type (type) of a variable? E.g. what is the type of myVar?
typeof myVar; (no need for parens since typeof is an operator, not a function). Thus, alert(typeof myVar);
when should you set a variable to ‘null’
When you expect it to contain a reference to an object. That way, you can explicitly check for the value null to see if the var has been filled with an object reference.
Why is this a good/bad idea:
if (a + b == 0.3) { do something… }
Bad idea because of how EMCAScript rounding errors with floating numbers. 0.1 + 0.2 will equal 0.300000000003 not just 0.3.
true/false: NaN == NaN
false.
How can you determine if something is NaN?
use isNaN() function.
How best to convert something to a number or cast something as a number?
Depends, but usually use parseInt() rather than Number() since Number() has some unexpected values. For example: Number(“”) will return zero rather than NaN. Number(“023blue”) will be NaN whereas parseInt(“023blue”) will be 23.
what is a “unary” operator?
only operates on one value, like ++ or – as in ++var.
var a = 10; alert( a++ + 10) gives you what?
20 because the ++ happens after the operation.
var a = 10; alert(++a + 10) gives you what?
21 because the ++ happens before the operation.
var a = false. What is a++?
numeric 1
var a = “1”; What is +a?
numeric 1
10 % 9 = ?
1
How can you find which word comes first, alphabetically, between “Brick” and “alphabet”?
“Brick”.toLowerCase() < “alphabet”.toLowerCase();
// false, as expected. Or
“Brick”.localeCompare(“alphabet”);
10 % 2 = ?
0
10 + “5” = ?
105
5 - “2” = ?
3 because “2” is converted to 2.
“a” < “b” (true or false)?
true
“B” < “a” (true or false)?
true, because uppercase characters come first.
How is the identically equal operator different from the equal operator?
It does the same thing, but doesn’t convert operands before testing equality. E.g., “55” === 55 will be false. However (“5”-2) === 3 will be true.
What’s the difference between a do/while statement and just a while statement?
Do/while will execute at least once no matter what. While may never execute: var i = 10; do { i += 10; } while (i<5); // this will execute once and i will become 20.
What does the continue statement do to a loop?
It stops the action in the given iteration, but then continues going.