Language Basics Flashcards

1
Q

What does the function isFinite() do?

A

This function returns true if a value is finite (that is, it occurs between the minimum and maximum)

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

How can you get the largest and smallest number available?

A

Number.MAX_VALUE

Number.MIN_VALUE

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

What is wrong with this comparison?

a + b == 0.3

A

You should expect small rounding issues in JavaScript.

0.1 + 0.2 does not equal 0.3

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

How should you define a variable that is later meant to hold an object?

A

It is advisable to initialize the variable to null.

var car = null;

This allows you to explicitly check for the value null to determine of the variable has been filled with an object reference at a later time.

if (car != null) {
  // do something with car
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the special numeric value NaN used to indicate?

A

When an operation intended to return a number has failed.

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

What is unique about NaN

A
  1. Any operation involving NaN always returns NaN

2. NaN is not equal to any value, including NaN

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

How do you determine if a value is NaN?

A

isNaN() takes a single value, attempts to convert it into a number and returns true or false.

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

What does Number(“ “); return?

A

0 - empty strings return zero.

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

What does parseInt(“ “); return?

A

NaN, empty strings return NaN. Leading white space is ignored until the first non white space character is found. If that character isn’t a plus, minus, or number NaN is returned.

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

What does parseInt(“1234blue”) return?

A

1234, after parseInt() finds the first character and it’s a number, plus, or minus, the conversion then goes on to the second character and continues on until either it reaches the end of the string or a non numeric character.

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

What does radix mean.

A

Number of digits.

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

What is the second argument in parseInt() used for?

parseInt(“10”, 10);

A

parseInt() provides a second argument - the radix (base) to use.

Most of the time you’ll be parsing decimal numbers, so it’s good to include 10 as the second argument.

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

What’s the difference between parseFloat() and parseInt()?

A

In parseFloat() a decimal point is a valid character the first time it appears.

Also, in parseFloat() initial zeros are always ignored. And there is no radix mode - so all hex numbers become 0.

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

does toString() take any arguments?

A

In most cases, no. But, when used on a number value, toString() accepts a single argument: the radix in which to output the number.

var num = 10;
num.toString(2); // output 10 in binary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are unary operators?

A

Operators that work on only one value.

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

How do you increment a variable in short hand?

A
var age = 29;
\++age;

The variable’s value is changed before the statement is evaluated.

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

How do you decrement a variable in short hand?

A
var age = 29;
--age;

The variable’s value is changed before the statement is evaluated.

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

What is the difference between age++ and ++age?

A

When using postfix increment (age++), the increment doesn’t occur until after the containing statement has been evaluated.

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

What’s the value of num3?

var num1 = 2;
var num2 = 20;
var num3 = num1-- + num2
A

22

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

What’s the value of num3?

var num1 = 2;
var num2 = 20;
var num3 = --num1 + num2
A

21

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

What does this return?

+“01”

A

1

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

What does this return?

+false

A

0

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

What does this return?

+true

A

1

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

Negative numbers are stored in binary code in a format called what?

A

two’s complement

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

How is the two’s complement of a number calculated?

A
  1. determine the binary representation of the absolute value
  2. find the one’s complement of the number (every 0 becomes a 1 and 1 becomes a 0)
  3. add 1 to the result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How many bits are integers in JavaScript?

A

32 bits

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

How many bits are used to represent the numeric value of an integer in JavaScript?

A

31 bits. In JavaScript integers are 32 bits and the first bit - the sign bit - is used to represent the sign of the integer.

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

What format are numbers stored in ECMAScript?

A

IEEE-754 64-bit format. However, bitwise operations do not work directly on the 64-bit representation, instead the value is converted to a 32-bit format

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

What happens if a bitwise operator is applied to a nonnumeric value?

A

The value is first converted into a number using Number(), then the bitwise operation is applied.

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

What two special values are treated as the equlivant of 0 when used in bitwise operations?

A

NaN and Infinity. This is a side effect of the conversion between 64-bit and 32-bit number representations.

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

What is the bitwise NOT represented as?

A

a tilde ( ~ )

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

What does the bitwise NOT do?

A

returns the one’s complement of the number. It negates the number and subtracts one.

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

What is the value of num2?

var num1 = 25;
var num2 = ~num1;
A

-26

34
Q

What is the bitwise AND represented as?

A

an ampersand ( & )

35
Q

What is the bitwise OR represented as?

A

a single pipe ( | )

36
Q

What is the bitwise XOR represented as?

A

a caret ( ^ )

37
Q

What is the Left Shift represented as and what does it do?

A

two less-than signs ( &laquo_space;)
It shifts all bits in a number to the left by the number of positions given.
It also preservers the sign of the number.

38
Q

What is the Signed Right Shift represented as and what does it do?

A

two greater-than signs (&raquo_space; )

It shifts all bits to the right while preserving the sign. It’s the exact opposite of the Left Shift.

39
Q

What is the Unsigned Right Shift represented as and what does it do?

A

three grater-than signs (&raquo_space;> )
It shifts all bits to the right by the given number. For numbers that are positive, the effect is the same as the signed right shift. For numbers that are negative, the operator considers the binary representation of the negative number to be a positive number instead. Because the binary representation is the two’s complement of it’s absolute value, the number becomes very large.

40
Q

What is the logical not operator represented as?

A

( ! )

41
Q

Why would you do this:

!!0;

A

To convert a value into it’s binary equivalent.

!!0 => false

42
Q

What is the logical AND operator represented as?

A

( && )

43
Q

What is the logical OR operator represented as?

A

( || )

44
Q

What is a short-circuited operation?

A

if the first operand determines the result, the second operand is never evaluated.

45
Q

What is the value of result?

var result = 5 + “5”;

A

“55”

46
Q

What is the value of result?

var result = 5 - “”;

A

5 because “” is converted to 0

47
Q

What is the value of result?

var result = 5 - null;

A

5 because null is converted to 0

48
Q

What is the operator for less-than-or-equal-to?

A

( <= )

49
Q

What is the operator for greater-than-or-equal-to?

A

( >= )

50
Q

What is the value of result?

var result = “Brick” < “alpha”;

A

true.

Uppercase letters have lower character codes than lowercase letters.

51
Q

What is the value of result?

var result = “23” < “3”;

A

true.

Because both operands are string, they are compared by their character codes.

52
Q

What is the value of result?

var result = “23” < 3;

A

false.

Because there is a number all strings are converted to numbers.

53
Q

What is the difference between ( == ) and ( === )?

A

( === ) does not convert operands before testing for equality.

54
Q

What is the value of result?

var result = (“55” === 55);

A

false - not equal because of different data types

55
Q

What is the value of result?

var result = (“55” == 55);

A

true - equal because of conversion

56
Q

How do you use the conditional operator?

A

variable = boolean_expression ? true_value : false_value;

57
Q

What is the compound-assignment operator for adding?

A

+=

58
Q

How can you assign multiple variable in a single statement?

A
Use the comma operator.
var num1=1, num2=2, num3=3;
59
Q

What is num assigned to?

var num = (5, 2, 3, 2, 0);

A

0 because it is the last item in the expression.

60
Q

What is a post-test loop?

A

A loop where the escape condition is evaluated only after the code inside the loop has been executed. e.g.) do-while

61
Q

When are post-test loops used?

A

They are most often used when the body of the loop should be executed at least once before exiting.

62
Q

What is the basic syntax for a for loop?

A

for (initialization; expression; post-loop-expression) statement

This runs only if the expression evaluates to true

63
Q

How can you create an infinite loop in a for statement?

A

By omitting the loop’s initialization, control expression, and postloop expression.
for (;;) {
doSomething();
}

64
Q

What’s the syntax for the for-in loop?

A

for (property in expression) statement

for (var propName in window) {
document.write(propName);
}

65
Q

What is the for-in loop used for?

A

It is used to enumerate the properties of an object.

66
Q

What is a labeled statement used for and what does the syntax look like?

A

It is possible to label statements for later use with the following syntax:

label: statement

start: for (var i=0; i < count; i++) {
  alert(i);
}

In this example the label start can be referenced later by using the break or continue statement. Labeled statements are typically used with nested loops.

67
Q

How can you reference a label with a break or continue?

A

break labelName;

continue labelName;

68
Q

What does the with Statement do and what does the syntax look like?

A

sets the scope of the code within a particular object.

with (expression) statement;

69
Q

Give an example of a common use of the with statement.

A

Note: considered bad practice to use width in production code because of performance implications and difficulty debugging.

With was created as a convenience for times when a single object was being coded over and over again, such as this example:

var qs = location.search.substring(1);
var hostName = location.hostname;
var url = location.herf;
with(location){
  var qs = search.substring(1);
  var hostName = hostname;
  var url = herf;
}
70
Q

Give an example of the syntax of the switch statement.

A
switch (expression) {
  case value: statement
    break;
  case value: statement
    break;
  case value: statement
    break;
  default: statement
}
71
Q

In a switch statement, if you need a case statement to fall through what should you do?

A
Include a comment indicating that the omission of the break statement is intentional:
...
case 25:
  /* falls through */
case 35:
...
72
Q

How does the switch statement compare values (does it use type conversion)?

A

The switch statement compares values using the identically equal operator (no type conversion).

73
Q

What is the basic syntax of a function?

A
function functionName(arg0, arg1,...argN) {
  statements
}
74
Q

Can a function only return a value sometimes?

A

Yes. But it is considered best practice to make sure that your functions either always return a value or never return a value. (helps with debugging)

75
Q
function(name, message){
...
}

How many arguments can you send this function?

A

As many as you want.

76
Q

How can you access an array of arguments with a function?

A

arguments are within the arguments array. You can access the first argument with arguments[0]

77
Q

How can you check the amount of arguments passed into a function?

A

arguments.length within the function

78
Q
function test(first, second){
...
}

test(1);
What is the value of second inside the function?

A

undefined. This is the same as initializing a variable without assigning it a value.

79
Q

What happens if two functions have the same name?

A

The last function becomes owner of the name.

80
Q

What is the difference between floats and integers?

A

There is no separate data type for integers versus floating point values; the Number type represents all numbers.

81
Q

What is the base of all objects?

A

The data type Object

82
Q

Do you need to specify a return value of a function?

A

No.