Types and Operators Flashcards
What is dynamic typing?
You don’t tell the JS engine what type of data a variable holds. Instead, it figures it out while your code is running. Variables can hold different types of values because it’s all figured out during execution.
Other languages often use Static Typing, which makes you define the data type when you declare the variable. For example, this would cause an error:
bool isNew = ‘hello’; //error
What is a primitive type?
A type of data that represents a single value, ie not an Object.
Name and define the six primitive types.
- Undefined - represents a lack of existence. This is what JS sets variables to by default initially and so you should never set your variables to undefined to avoid confusion.
- Null - also represents lack of existence. You should use this when you want to set them equal to nothing.
- Boolean - true or false.
- Number - Floating point number (there’s always some decimals). Unlike other programming languages, there’s only one ‘number’ type…and it can make math weird.
- String - A sequence of characters (both single and double quotes can be used).
- Symbol - Used in ES6, provide a way to attach properties to an object with more privacy and can be used with unique values.
What is an operator?
Operators are actually functions that are built into the JS engine by default, they just appear syntactically different.
Generally, operators take two parameters and return one result. JS operators use infix notation, which means that the operator sits between the two parameters and makes it more human-readable, but it’s still behaving otherwise like a function.
infix notation: 3 + 4;
prefix notation: +3 4;
postfix notation: 3 4+
What is operator precedence?
This defines the order in which operator functions get called. Funcations with higher precedence are called first.
What is associativity?
This defines the order operator functions get called in(left-right or right-left) when functions have the same precedence.
What will this output to the console?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/222/948/618/q_image_thumb.png?1508456816)
They are all equal to 4 because of associativity. The assignment operator works right-left and so the process looks like this to the JS engine:
a = b = c; (b = c is like the assignment function is running and returns 4)
a = b(4);
a = 4;
![](https://s3.amazonaws.com/brainscape-prod/system/cm/222/948/618/a_image_thumb.png?1508456868)
What is coercion?
Converting a value from one type to another.
What does this code output?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/223/013/045/q_image_thumb.png?1508532984)
This happened because the first paramer, 1, was coerced to a string.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/223/013/045/a_image_thumb.png?1508532992)
What should this output and why?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/223/013/296/q_image_thumb.png?1508604815)
It should output true. The reason is that the < operator has left-to-right associativity. Also, the < operator is built to return a boolean. So, to the JS engine, parsing 3 < 2 < 1 would look like this:
- 3 < 2 < 1 - 3 is not less than 2, so it returns false for that comparison.
- false < 1, now it will need to coerce false to a number in order to complete the comparison and false becomes 0.
- 0 < 1, now this will return true.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/223/013/296/a_image_thumb.png?1508604848)
What will Number(undefined) return?
NaN, or not a number. This is not a primitive type, but you can treat it as such - it’s essentially saying this cannot be converted into a number.
What will Number(null) return?
It will return 0. So, while null and undefined are pointing to the same value, to the javascript engine null can be converted to a number, unlike undefined which returns NaN.
What will this code return?
false == 0
True
What will this code return?
null == 0
False
What will this code return?
null < 1
True