Language Basics Flashcards
What does the function isFinite() do?
This function returns true if a value is finite (that is, it occurs between the minimum and maximum)
How can you get the largest and smallest number available?
Number.MAX_VALUE
Number.MIN_VALUE
What is wrong with this comparison?
a + b == 0.3
You should expect small rounding issues in JavaScript.
0.1 + 0.2 does not equal 0.3
How should you define a variable that is later meant to hold an object?
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 }
What is the special numeric value NaN used to indicate?
When an operation intended to return a number has failed.
What is unique about NaN
- Any operation involving NaN always returns NaN
2. NaN is not equal to any value, including NaN
How do you determine if a value is NaN?
isNaN() takes a single value, attempts to convert it into a number and returns true or false.
What does Number(“ “); return?
0 - empty strings return zero.
What does parseInt(“ “); return?
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.
What does parseInt(“1234blue”) return?
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.
What does radix mean.
Number of digits.
What is the second argument in parseInt() used for?
parseInt(“10”, 10);
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.
What’s the difference between parseFloat() and parseInt()?
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.
does toString() take any arguments?
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
What are unary operators?
Operators that work on only one value.
How do you increment a variable in short hand?
var age = 29; \++age;
The variable’s value is changed before the statement is evaluated.
How do you decrement a variable in short hand?
var age = 29; --age;
The variable’s value is changed before the statement is evaluated.
What is the difference between age++ and ++age?
When using postfix increment (age++), the increment doesn’t occur until after the containing statement has been evaluated.
What’s the value of num3?
var num1 = 2; var num2 = 20; var num3 = num1-- + num2
22
What’s the value of num3?
var num1 = 2; var num2 = 20; var num3 = --num1 + num2
21
What does this return?
+“01”
1
What does this return?
+false
0
What does this return?
+true
1
Negative numbers are stored in binary code in a format called what?
two’s complement
How is the two’s complement of a number calculated?
- determine the binary representation of the absolute value
- find the one’s complement of the number (every 0 becomes a 1 and 1 becomes a 0)
- add 1 to the result
How many bits are integers in JavaScript?
32 bits
How many bits are used to represent the numeric value of an integer in JavaScript?
31 bits. In JavaScript integers are 32 bits and the first bit - the sign bit - is used to represent the sign of the integer.
What format are numbers stored in ECMAScript?
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
What happens if a bitwise operator is applied to a nonnumeric value?
The value is first converted into a number using Number(), then the bitwise operation is applied.
What two special values are treated as the equlivant of 0 when used in bitwise operations?
NaN and Infinity. This is a side effect of the conversion between 64-bit and 32-bit number representations.
What is the bitwise NOT represented as?
a tilde ( ~ )
What does the bitwise NOT do?
returns the one’s complement of the number. It negates the number and subtracts one.