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.