Operators, data types, some syntax Flashcards
How can you include an external javascript file in HTML?
/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?
6 simple: undefined typeof 'undefined' boolean typeof 'boolean' number typeof 'number' string typeof 'string' bigint typeof 'bigint' symbol typeof 'symbol'
3 complex object typeof 'object' null typeof 'object' function typeof 'function'
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, EMCAScript rounding error :floating number. 0.1 + 0.2 = 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