General - Beginning Flashcards
Name Three ways to define variables in JavaScript
var, let, const
Give the definition of an “operator” in JavaScript.
An operator is a character or phrase that performs a task in our code.
typeof is an operator
(Arithmetic operators: + - * / %, etc)
In a general sense, what is a “property” of a data type in JavaScript
Property in regards to data types would be information automatically generated about the data type in question.
What is a method in JavaScript?
A method is a function that is a property of a Primitive Type or an Object.
How do we invoke or call a method in JavaScript.
Methods are attached to their Primitive Type or Object with the dot operator (.) and Empty Parenthesis ().
What are the 8 fundamental data types in JavaScript?
- String
- Number
- Boolean
- Null
- Undefined
- Symbol
- BigInt
- Object
What is a mathematical assignment operator?
Mathematical assignment operators are built-in operators in javascript that help with incrementation. For example:
+=
-=
*=
/=
What is the equivalent mathematical assignment operator to z = z + 1?
z += 1
What is the z *= 2 equivalent to without using a mathematical assignment operator?
z = z * 2
z = 5
z -= 1
z = ?
z = 4
z = 6
z /= 2
z = ?
z = 3
What is the increment operator in JavaScript and how is it used?
++
This mathematical assignment operator increase the value of something by 1
What does the following represent and what is its function?
–
This is the decrement operator.
The decrement operator is a mathematical assignment operator that decreases the value of something by 1
How can we check the type (data type) of a variable
We can use the ‘typeof’ operator in JavaScript.
console.log(typeof variableName)
What are the two broad types of Methods in JavaScript.
Instance Methods - methods called on an object instance
Static Methods - methods called on an object constructor, not on an instance.
What does it mean that JavaScript is weakly typed?
To be weakly typed means that we do not need to declare the type of data being stored in a variable.
What is a conditional statement in JavaScript
A statement that checks a certain condition and performs an action based on that condition
Give the syntax of an ‘if’ statement
if(condition){code block/block statement}
if condition evaluates to true, the code block will be executed
Where would we use an if…else statement
When we have a binary decision that can evaluate to true or false
What is the syntax of an else statement (assume the presence of an if statement)
else{code block/block statement}
when the if-statement evaluates to false the else code block will execute.
List the comparison operators
- < less than
- > greater than
- < = less than or equal to
- > = greater than or equal to
- === strictly equal to
- ! == not strictly equal to
What is the identity operator?
===
What is the equality operator?
==
How do the identity operator and equality operator differ
The equality operator ( == ) attempts to unify the data-types before comparison
The identity operator ( === ) will not attempt type conversion before comparison
Name three logical operators
&& = and
|| = or
! = not
How does the logical operator ‘or’ process a set of conditions?
The || operator evaluates the first conditional, if ‘true’ it does not check the second condition.
If the first conditional is false, then the || operator checks the second condition
How does the ‘not’ operator affect a condition?
The ! operator reverses the boolean value of a condition.
Give examples of ‘falsy’ values
- 0
- ‘’ or “”
- NaN
- undefined
- null
How does the logical operator ‘and’ work?
The && operator evaluates both conditionals and if both are truthy returns the last conditional
What is short circuit evaluation?
Short circuit evaluation uses logical operators to make code more concise (but potentially less legible).
Given an example of using || in a short circuit evaluation.
let userName = ‘’
let welcome = userName || ‘Stranger’
will set welcome to ‘Stranger’ since userName is falsy.
Give an example of short circuit evaluation using && operator.
hasFireworks && makeFestive()
if hasFireworks is true then the makeFestive function will be called (because it is truthy).
What is the ternary operator in JavaScript
? = ternary operator?
How do we use the ternary operator?
The ternary operator can replace an if…else statement.
[if statement] ? [ifTrueCode] : [ifFalseCode]
What’s the difference between a relative and absolute file path?
A relative path references the file based on the current directory
An absolute path references the file based on the root or HTTP location
How do you escape a character in JavaScript?
\ - backslash
What is a JavaScript expression?
An expression is a valid set of literals, variables, operators, and expressions that evaluate to a single value.
What is the assignment operator?
= - the equal sign, because it is used to assign something a value
If an if or else…if statement evaluates to true does the code block continue to be read?
No, the code stops at the true conditional and exits the conditional flow.
How is switch used for conditional evaluation?
switch can be used to replace multiple if, else…if, else conditionals.
Give the syntax of a switch conditional statement.
switch(variableToExamine){
case ‘condition1:
do code
break
case ‘condition2:
do code
break
default:
do code
break
}
Will the switch conditional statement exit code upon a truthy result?
No, without a break statement following a conditional check, all other conditions will be evaluated.
What is it called when JavaScript attempts type conversion on the comparison of values?
Casts or casting
What is a common use case for the NOT operator?
It is often used to eliminate the else statement in a conditional.
if(!variable){ do something if variable is empty}
What does the value of undefined indicate?
Basically it indicates the lack of a value.
We say something is undefined when it is not explicitly defined
What does the value of null indicate?
Null indicates the intentional lack of a value.
Developers need to intentionally set a value to null.
How to null and undefined compare using strict equality comparison.
They are not equal because they are different primitives.
What is scope in relation to variables?
Scope is the context in which our variables are declared and in which context they are available
Where are variables in the global scope defined?
Things in the global scope are defined outside of function code blocks.
Where are globally scoped variables able to be accessed?
Globally scoped variables can be accessed within any code block in the program.
What is block scope in relation to variable declaration in JavaScript?
Block scope indicates that the variable is declared within a block of code.
Where is a block scoped variable available
It is available within the block of code in which it is declared (basically within the curly braces)
Where do globally defined variables ‘live’ in our code?
They reside in the ‘Global Namespace’
What can too many variables in the Global Namespace cause and why is it undesirable?
They can cause Scope Pollution
It is undesirable because it can be hard to keep track of many variables and there can be unexpected consequences of collisions an accidents.
To avoid Scope Pollution what should we do with our variables?
We should scope them as tightly as possible.
What are four benefits of tightly scoped variables?
- Legibility
- Clarity
- Maintainability
- Performant