The Basics Flashcards
What do data types represent?
Different types of data.
They help programmers and their programs determine what they can and cannot do with a given piece of data.
What are the 5 primitive data types?
String
Number
Undefined
Null
Boolean
*Symbol (ES6)
*BigInt (ES6)
If it is not a primitive type?
It is an object type
If it is a built-in feature of JavaScript, should we use capitalizds or uncapitalized names?
Capitalized
*Exception: typeof operator (it always returns a lowercase string value)
What is a ‘literal’?
A literal is any notation that lets you represent a fixed value in source code
String
A string is a list of characters in a specific sequence.
How do you write string literals?
With either single quotes (‘ ‘) or double quotes (“ “)
- note that the quotes are syntactic components, not part of the value.
Escape character or backlash ‘ \ ‘ ?
The backslash, or escape character (), tells the computer that the next character isn’t syntactic, but is part of the string
Escaping a quote character prevents JavaScript from seeing it as the end of the string
Template literals
They use backticks (`) and enable an operation called string interpolation
Number
The Number data type represents all kinds of numbers in JavaScript
Booleans
Boolean values represent an “on” or “off” state
There are two boolean literal values: true and false:
Undefined
When a variable is not defined, its value is given by undefined.
We can describe undefined as representing the absence of a value.
We can also explicitly use the literal undefined.
What does ‘console.log()’ return?
console.log writes a value to the console.
However, it doesn’t have a reason to return anything specific.
Instead, it returns undefined.
Null
it represents the intentional absence of a value.
It represents emptiness or nothing
Null vs Undefined
You must use null explicitly if you want to use it; undefined can arise implicitly.
typeof operator
typeof returns a string that contains the type of its operand’s value.
typeof null
‘object’
*Mistake from the first version of JavaScript
typeof array
‘object’
in JavaScript, arrays are objects (with some special characteristics)
Arithmetic operators in JS
- Addition: ‘ + ‘
- Subtraction: ‘ - ‘
- Multiplication: ‘ * ‘
- Division: ‘ / ‘ (integers or decimals)
- Remainder operator: ‘ % ‘ (works with decimal numbers as well)
Remainder operator
computes the remainder of dividing two numbers
Remainder operator vs Modulo operator
Remainder operations return a positive integer when the first operand is positive, and a negative integer when the first operand is negative.
Modulo operations return a positive integer when the second operand is positive, and a negative integer when the second operand is negative.
Does JS have an built-in modulo operator or method?
NO!
NaN
This special value signals an illegal operation on numbers
What two main situations may cause a NaN result?
- Undefined mathematical operations, such as dividing 0 by 0 or trying to take the square root of a negative number, return NaN.
- Trying to convert a non-number value, such as ‘hello’, to a number can also return NaN.
typeof NaN
‘number’
NaN is a numeric result that indicates an error occurred
Why can’t I determine if a value is NaN with regular comparison operator?
NaN is the only value in JavaScript that is not equal to itself
How can I check if a value is NaN?
Number.isNaN or Object.is
Infinity
Infinity is a number that is so large, it can’t be written down.
One common operation that results in Infinity is to divide a positive number by 0
-Infinity?
-Infinity represents an infinite value that is less than 0. It most commonly arises when dividing a negative number by 0:
typeof Infinity and typeof -Infinity?
‘number’ (just like NaN)
Can you use comparison operators ‘ === ‘ with Infinity and -Infinity?
YES!
However, be wary of using expressions in such comparisons:
In JS, how do you compare if two values are identical?
With the ‘ === ‘ operator
What is string concatenation?
The result of using the ‘ + ‘ operator to join two strings
What is implicit type coercion?
When using +, if either operand is a string and the other is not, JavaScript coerces the non-string operand to a string; thus, the result is always another string.
What happens, though, when you perform some other arithmetic operation on a string and number?
What is explicit type coercion?
The difference between explicit and implicit coercion is that explicit coercion lets you decide what you want to do, whereas implicit coercion lets the engine choose.
How can I coerce a string to a number?
Number function:
What happened here?
In situations where most other languages raise an error, JavaScript fails silently.
Can you use parseInt function to coerce a String into a Number?
YES!
A surprising feature of parseInt is how it handles a string whose value begins with a numeric digit or a + or - sign followed by a digit.
In both cases, it returns a numeric value, even though the remaining characters may not be numeric.
It stops converting and ignores everything else once it encounters an invalid character:
What does parseInt return if the number in the string is too big or too small to represent as a JS number?
It returns Infinity
parseFloat
It coerces a string to a floating-point (decimal) number.
Can you coerce a number into a string?
YES!
The String function provides this capability.
Arrays (complex data type)
JavaScript organizes information into ordered lists using arrays.
They may contain strings, numbers, booleans, or any other data type.
In JavaScript, array literals use square brackets [ ] surrounding a comma-delimited list of values, otherwise known as elements.
How do you access elements in arrays?
You can access an array element by placing its index inside brackets ( [ ] ).
Element indexes are non-negative integers starting with 0.
Trailing comma optional?
YES!
But recommended
Arrays three most important facts
- The order of the elements is significant.
- Use index numbers to retrieve array elements.
- Index numbers are non-negative integers starting from 0.
Objects in JavaScript
Objects are a dictionary-like data structure that matches keys with specific values.
JavaScript object is a collection of key-value pairs.
How do you create an Object?
You can create objects using object literals, which have zero or more key-value pairs separated by commas all embedded within curly braces ({}).
A key-value pair associates a key and a given value.
Each pair consists of a key, in the form of a string, and a value of any type.
Key-value pairs in object literals use the key followed by a colon (:) and then the value.
How do you retrieve a value in Objects?
We retrieve it by its key:
Note how similar the [‘cat’] notation is to indexing with arrays. Instead of index values, though, we use key names in string form.
What are expressions in JavaScript?
An expression is anything that JavaScript can evaluate to a value, even if that value is undefined or null.
They evaluate to a value that can be captured and used in subsequent code.
Do expressions have to use operators?
NO!
Expressions don’t have to involve operators: any value is an expression that evaluates to itself:
Printing (logging) vs returning values
When we invoke the console.log method, we’re telling JavaScript to write something to the console.
Expressions do something, but they also return or evaluate to a value.
What happened here?
Here, console.log displayed “Howdy” on the console, but then it showed the word undefined in a different color or dimmer brightness.
That extra output is the return value of the expression, and undefined tells you that console.log returned nothing.
It’s important to understand that distinction.
What happened here?
The value returned by console.log(“Howdy”) is undefined, so that’s the value to which a gets assigned.
Therefore, a on the second line evaluates to undefined, and node shows it as the return value.
What are statements in JavaScript?
Statements often include expressions as part of their syntax, but the statement itself is not an expression – its value cannot be captured and reused later in your code.
An example of a JavaScript statement is the variable declaration:
On the other hand, the value 3 to the right of the = is an expression
Is ‘foo’ here an expression?
YES!
Statements vs Expressions
The key difference between a statement and an expression is that you can’t capture a value from a statement.
For instance, we can capture and print the value of the expression 3 * 5 in the following call to console.log:
Can we replace 3 * 5 with a let declaration or while loop?
NO! Since they are both statements.
What is a statement?
A statement is a line of code commanding a task. Every program consists of a sequence of statements.
By this definition, every chunk of code that can be treated as a single unit is a statement.
What do statements include?
- variable, function, and class declarations
- loops and if statements
- return and break statements
- assignments: a = 3;
- standalone expressions: console.log(“Hello”);