Operators, data types, some syntax Flashcards

1
Q

How can you include an external javascript file?

A

/script src=’myfile.js’/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s better, putting scripts just before the closing body tag or in the head with ‘defer’ attribute?

A

Sort of a wash, but old browsers don’t understand defer so before probably better

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ECMAScript has how many data types?

A

5 simple: undefined, null, number, boolean, string;

and 1 complex: object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how do you find the data type (type) of a variable? E.g. what is the type of myVar?

A

typeof myVar; (no need for parens since typeof is an operator, not a function). Thus, alert(typeof myVar);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

when should you set a variable to ‘null’

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is this a good/bad idea:

if (a + b == 0.3) { do something… }

A

Bad idea because of how EMCAScript rounding errors with floating numbers. 0.1 + 0.2 will equal 0.300000000003 not just 0.3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

true/false: NaN == NaN

A

false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you determine if something is NaN?

A

use isNaN() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How best to convert something to a number or cast something as a number?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a “unary” operator?

A

only operates on one value, like ++ or – as in ++var.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
var a = 10;
alert( a++ + 10) gives you what?
A

20 because the ++ happens after the operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
var a = 10;
alert(++a + 10) gives you what?
A

21 because the ++ happens before the operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

var a = false. What is a++?

A

numeric 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

var a = “1”; What is +a?

A

numeric 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

10 % 9 = ?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you find which word comes first, alphabetically, between “Brick” and “alphabet”?

A

“Brick”.toLowerCase() < “alphabet”.toLowerCase();
// false, as expected. Or
“Brick”.localeCompare(“alphabet”);

17
Q

10 % 2 = ?

A

0

18
Q

10 + “5” = ?

A

105

19
Q

5 - “2” = ?

A

3 because “2” is converted to 2.

20
Q

“a” < “b” (true or false)?

A

true

21
Q

“B” < “a” (true or false)?

A

true, because uppercase characters come first.

22
Q

How is the identically equal operator different from the equal operator?

A

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.

23
Q

What’s the difference between a do/while statement and just a while statement?

A
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.
24
Q

What does the continue statement do to a loop?

A

It stops the action in the given iteration, but then continues going.

25
Q

In a for loop, what is the term for parts a, b, and c?

for (part a, part b, part c){ // do something }

A

part a is the initialization; part b is the expression; part c is the post loop expression.

26
Q

what does the break statement do to a loop?

A

It not only stops the action in a given iteration, it stops the entire loop.

27
Q

In a switch statement, how can you test if variable var equals 10?

A

switch (var) { case 10: // do something then break

28
Q

In a switch statement, how can you test if variable var is between 10 and 20?

A

declare var outside of statement. Then say: switch(true) { case var >10 && var < 20: // do something then break

29
Q

How could you find how many arguments have been passed to a function?

A

With the arguments object, used within the function:

arguments.length;

30
Q

var n = null; what is typeof n?

A

object. Null is a primitive datatype, but it is also an object.

31
Q

why does typeof null === “object” return true?

A

because of how js was made in the beginning, it returns this “bogus” value (according to MDN).

32
Q

var x, y, z;
x = ( y = 1, z = 4 );
What happens here?

A

Comma operators go from left to right and return value on right.
x will = 4; y = 1; z = 4;.

33
Q

var x, y, z;
x = y = 1, z = 4;
x = ( y = 1, z = 4 );
What’s the difference?

A

Comma operators have the least precedence of all operators, so top sets x and y both to 1. z is 4.
Bottom sets x to 4 because parens require everything inside get evaluated first.