JavaScript 210 - Basics
Why is '50' < '6'?
'5' precedes '6' lexicographically
What is the difference between a++ and ++a?
- If the operator appears after the operand, JavaScript evaluates the expression, then modifies the operand. - If the operator appears before the operand, JavaScript modifies the operand, then evaluates the expression
What are the 5 primitive Data Types in JavaScript (ES5)?
- Number - Boolean - String - Null - Undefined
What is the Operator precedence for JavaScript?
- Parenthesses - Exponent - Multiplication - Division - Addition - Subtraction
- 1 + true Why does this evaluate to 2? '- 4' + 3 Why is this 43? - false == 0 Why is this true?
- true is coerced to the number 1, so the result is 2 - 3 is coerced to '4', so the result is 43 - 0 is coerced to false, so the result is true
Why does the last line give an error? var a; // a statement to declare variables var b; var c; var b = (a = 1); // this works, because assignments are expressions too var c = (var a = 1);
A Statement cannot be used as part of an expression.
Infinity -Infinity NaN
Infinity - When a number a greater than any other number. - Infinity - When a number is less than any other number NaN - Not a Number - This is an error for a math function.
What happens when you company NaN to itself or any value?
It always returns false. NaN is the only value not equal to itself.
Other programming languages have distinct data types to represent integer, float, double, real, or decimal values. What is the difference between JavaScript and other languages when it comes to the data types listed above?
JavaScript uses a floating point system to represent all numbers.
Which values are considered falsey?
Null NaN 0 '' Undefined
Explain Primitive values assigned to a variable. Do the values actually change?
Primitive values do not change, the variable assigns new values to variables that used to contain different values.
Logical And &&? What is true about this operator?
Returns true if both operands are true or both operands are false. If the first operand can be converted to false, then the whole expression is false.
Logical Or ||? What is true about this operator?
Returns true if one operand is true, false otherwise.
false || []; // [] (second operand is non-boolean, it is returned as is) Why is the second operand returned as is?
Since the first operand was false, [] is returned true because it's a non-boolean type.
var x = 2; Which is a statement and expression?
The Var declaration is a statement and the assignment to 2 is an expression
What happens when one of the operands is an object and both operands are added together?
The operands are converted into strings and added
What is the difference between the + operator and the other operators?
The other operators will coerce strings to numbers.
123 > 'a' What is the result of this expression and why?
The result is false because 'a' is coerced to 'NaN'. Any number comparison with NaN is false.
11 > '9' What is the result of this expression and why?
The result is true because because "9" is coerced to 9.
What happens when both operands are a combination of numbers, booleans, nulls, or undefined and are added together?
They are converted into numbers and added together.
All JavaScript primitive are immutable. What does this mean?
This means you cannot change them once you create them.
Expression. Give four examples of expressions - String - Arithmetic - Assignment - Logical
Valid code that resolves to a value. - String - expressions that evaluate to a character string - Arithmetic - expressions that evaluate to a number - Logical - expressions that evaluate to true or false.
When it comes to double quotes (""), what are the rules?
You cannot have double quotes within single quotes and you cannot have double quotes within double quotes.
a. == b. != c. === d. !== e. > f. <
a. The Operands are equal regardless of type b. The Operands are not equal regardless of type c. The operands are equal in value and type d. Returns true if the operands are not equal and/or not of the same type e. Returns true if the left operand is greater than the right f. Returns true if the left operand is less than the right
What is the compound data type in JavaScript?
object