JavaScript Flashcards
what does the console keyword refer to?
an object, collection of data and actions that can be used in our code
how do you print in javascript?
console.log( ) ;
how do you write a single line comment in JavaScript?
with two forward dashes //
How do you write a multi-line comment in JavaScript?
/ *
- /
What are data types?
classifications given to different kinds of data used in programming
What are the 8 fundamental data types in JavaScript?
number (any number, including decimals)
string (any grouping of characters surrounded by single quotes) (string can be thought of as a fancy word for text)
boolean: data type with only two values - true or false
null: intentional absence of a value
undefined: also represents absence of a value, with different uses to null
symbol: unique identifiers
object: collections of related data
BigInt: represents integers of arbitrary length
What are the data types number, string, boolean, null, undefined, and symbol referred to as?
primitive data types - the most basic data types in the JavaScript language
What is the difference between using console.log() to print numbers and text
text needs quotation marks around it within the brackets of console.log() whereas numbers do not
What is an operator in JavaScript?
a character that performs a task in the code
Give some arithmetic operators
\+ add - subtract * multiply / divide % remainder
What is the remainder operator sometimes called?
modulo (although it’s not quite a modulo)
What does the remainder operator do?
prints the remainder (returns the number that remains after the right hand number divides into the left hand number
What is the process of appending one string to another called?
concatenation
What property stores the number of characters in a string?
length
What are methods?
actions we can perform
What does math.floor() do?
takes a decimal and rounds it to the nearest whole number
what does math.random() do?
generates a random number between 0 to 1
What is a variable?
a container for a value that is stored in the computer’s memory
named storage for data
What can you do with variables?
create a variable with a descriptive name
store or update information stored in a variable
reference or ‘get’ information stored in a variable
what keywords can you use to declare variables?
let and const
What is the conventional way of capitalising in JavaScript called?
Camel Casing
How does camel casing work?
all words are grouped into one long word, the first word is not capitalised, then every word that follows is capitalised
myName
camelCaseEverything
what is the operator ‘ = ‘ called?
the assignment operator
what does the assignment operator do?
it assigns a value to a variable
Can variable names start with numbers?
no
are variable names case sensitive?
yes
can variable names be the same as keywords?
no
what does the let keyword signal?
that a variable can be reassigned a different value
what happens if you don’t give a variable a value?
it automatically has a value of undefined
what is different about the variable const compared to let
a const variable (short for constant) cannot be reassigned another value, and must be assigned a value when declared
What is the increment operator and what does it do?
the increment operator is ++ and it increases the value of the variable by 1
What is the decrement operator and what does it do?
the decrement operator is – and it decreases the value of a variable by 1
what does interpolate mean?
insert
What can you use the typeof operator for?
to check the data type of a variable
What is a code block / block statement indicated by in JavaScript?
curly brackets { }
What is this comparison operator? ‘ === ‘
the identity operator
What do all comparison statements evaluate to?
true or false
What are all comparison operators made up of?
two values that will be compared
an operator that separates and compares the values
What are the operators that work with booleans called?
logical operators
What are the 3 logical operators?
&& - the and operators
|| - the or operator, also called the pipe operator
! - the not operator, also known the bang operator
When would you use the && (the and) operator?
to check if two things are true
What can you do if only one condition needs to be true?
the || (pipe / or operator)
What does the ! (bang, not) operator do?
reverses/negates the value of a boolean
take a true or false value and return the opposite
What values are falsy?
0, empty strings, null, undefined, NaN (not a number)
When does JavaScript assign a truthy value in a boolean condition?
when you use the || pipe operator
What is short-circuit evaluation?
the semantics of boolean operators in which the second argument is executed or evaluated only if the first argument does not suffice
What is a (conditional) ternary operator?
operator that takes 3 operands:
a condition, followed by a question mark ?, then an expression to execute if the condition is truthy followed by a colon, followed by an expression to excute if the condition is falsy
isNightTime ? console.log(‘turn on the lights’) : console.log(‘turn off the lights’);
Where does the ‘else if’ statement go?
After the if statement and before the else statement(s)
What can you use instead of a long list of ‘else if’ statements
switch
What should finish a case clause within switch?
break;
What is the syntax for switch?
switch (\_\_) { case '\_\_\_': console.log('\_\_\_\_'); break; //as many cases as need default: console.log('\_\_\_'); break;
what is a function?
a reusable block of code that groups together a sequence of statements to perform a specific task
give a way to create a function
with a function declaration
what does a function consist of?
the function keyword, the name of the function / its identifier followed by parentheses, a function body - the block of statements required to perform a task and enclosed in curly brackets
keyword identifier ( ) { }
what is the hoisting?
it allows access to declarations before they’re defined
is hoisting good practice?
no
when is the code inside a function executed
when it is called
How do you call a function?
type the function name followed by parentheses
functionName ()
what do the parameters of a function do?
allow functions to accept input(s) and perform a task using those inputs
they are treated like variables within a function
parameters act as ___ for values inside a function
placeholders
when calling a function with parameters, we ___ the values in the parameters
specify
values that are passed to the function when it is called are called ___
arguments
how can arguments be passed to a function
as values (eg numbers) or as variables (eg recWidth, recHeight)
what do default parameters do?
allow parameters to have a predetermined value in case there is no argument passed into the function / or if the argument is undefined when called
how do you pass back information from a function call?
with return, followed by the value you want to return
what happens if the value is omitted from return?
undefined is returned instead of the value wanted
what do you call functions being called within another function?
helper functions
what is usually omitted in a function expression?
function name
what is a function with no name called?
an anonymous function
how do you define a function inside an expression?
with the function keyword
what is the fat arrow notation?
=>
what do arrow functions do?
remove the need to type out the keyword function every time you need to create a function
how do you use and write functions with the arrow function?
include your function parameters inside brackets ( ) and then add a fat arrow => that points to the function body {
}
when are parentheses required with function parameters?
when the function has more than one parameter
does a one line function need curly braces?
no
what is an implicit return?
when a single line function follow the fat arrow notation, removing the need for the return keyword
What is initialization?
assigning an initial value to a variable
What is common practice when assigning constant variables?
naming the variable in all uppercase, with an underscore between words
const FAV_PET =
what are the limits on variable naming?
a variable name cannot start with a digit, and must be named with letters, digits, or the symbols $ or _
how are arrays written?
inside square brackets [ ]
array indexes are __ based?
zero based, meaning that the first item is 0, second is 1, and so on
how are JS objects written?
with curly brackets {
}
what are function arguments?
values received by the function when it is invoked / called
what happens to a function when JS reaches a return statement?
the function stops executing
what operator invokes/calls a function?
the parentheses operator ( )
what are local variables?
variables declared within a function
what does it mean that JavaScript is dynamically types?
that there are different types of data within the .js language, but that variables are not bound to any specific type of data
double and single quotes are ___ quotes
simple quotes
backtick quotes are ___ quote
extended functionality quotes
what is a JS expression?
a snippet of code that evaluates to a value
what is a JS expression?
a snippet of code that performs an action
when you write a JS function, all the arguments must be ___, not ___
all arguments must be expressions, not statements
what shows a template literal in JS?
backticks
what are the three methods for extracting part of a string?
slice (start, end)
substring (start, end)
substr (start, end)
what does slice ( ) do?
extracts part of a string and returns the expected part in a new string
what parameters does slice ( ) take?
start position and end position
what is the difference between slice( ) and substring( )
slice can take negative numbers as parameters, whereas substring cannot
what happens if you miss out the second parameter of substring( )
it will cut out the rest of the string from the position of the first parameter
what does trim( ) do?
removes whitespace from either side within a string
all comparison operators return a ___ value
boolean
for boolean values, true becomes ___
1
for boolean values, false becomes ___
0
=== is a ___ equality operator
=== is a strict equality operator
== is an ___ operator
== is an equality operator
when converted to a number, null becomes ___
null becomes 0 when converted to a number
when converted to a number, undefined becomes ___
undefined becomes NaN (not a number) when converted to a number
comparisons convert null to a __
comparisons convert null to a number (0)
what does an if statement need after it
brackets if ( ) { }
what are the four logical operators
|| or
&& and
! not
?? nullish coalescing
&& (AND) returns the first __ value, and || (OR) returns the first ___ value
&& (AND) returns the first falsy value, and || (OR) returns the first truthy value
the precedence of AND && operator is __ than OR ||
the precedence of AND && operator is higher than OR ||
the precedence of ! NOT is the ___ of all logical operators
the precedence of ! NOT is the highest of all logical operators
any value that is not false, undefined, null, 0, NaN, or an empty string ‘’ returns ___ when tested as a conditional statement
returns true
what is another word for function parameters?
argument (also properties and attributes)
how do you specify multiple arguments / parameters within a function
separate them with comments
how do you give a default parameter / argument to a function
by adding = after the name of the function
function hello ( name = ‘Chris’)
when do you often see anonymous functions?
when the (anonymous) function expects to receive another function as a parameter
what is scope?
scope defines where variables can be accessed or referenced
what is a block?
the code inside a set of curly braces { }
when you declare global variables, they go to the ___ ____
global namespace
what is scope pollution?
when too many global variables exist in the global namespace, or when variables are reused across different scopes
it’s best practice to define variables in the global scope, true or false?
false
it’s best practice to NOT define variables in the global scope.
why should you tightly scope your code?
- makes code more legible
- makes code more understandable
- makes code easier to maintain
- saves memory in code
what is an array represented by?
square brackets [ ]
what are the contents inside arrays called?
elements
what might the .push method also be referred to as?
a destructive array method
what is pass by reference?
when you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well
are elements declared within an array with a const variable mutable or constant
the elements within the array assigned to const variable are mutable
the elements within the array itself are mutable
if an array is passed through a function and mutated, is the mutation maintained outside the function?
yes
what is an array stored within another array called?
a nested array
how can you iterate a for loop through an array?
use the array’s .length property in the loop’s condition
in loop variable naming convention, what does i stand for
index
give a use for nested loops
to compare elements in two arrays
how does a nested loop execute?
for each round/iteration of the outer loop, the inner/nested loop will run completely
what does a do… while statement do?
executes a task once then keeps doing that task until condition is met
how is do… while different to a while loop?
a do while statement will run at least once regardless of whether the condition evaluates to true
what is a high-order function?
functions that accept other functions as arguments and/or return functions as output
what do arrow functions do?
remove the need to type function every time you create a function
in JavaScript, functions are __ class objects
first class objects
what does it mean that functions are first class objects?
JS functions can have properties and methods
what is a parameter in a function?
a placeholder for the data that gets passed into the function
what are functions that get passed into other functions as parameters called?
callback functions
what are iteration methods?
built-in JavaScript array methods that help us iterate
what are iterators?
methods called on arrays to manipulate elements and return values
what notation is used to designate an object literal?
{ } curly brackets
how is the data organised in JS objects?
in key-value pairs
what is a key in a key value pair?
it’s like a variable name that points to a location in memory that holds a value
what can a key’s value be?
any data type in the languages (including functions or other objects)
what is a key’s name known as?
its identifier
what is returned if you try to access a property that does not exist on an object?
undefined is returned
how do you access an object’s property?
with dot notation (object.property) or with bracket notation [ ]
when must you use bracket notation over dot notation for accessing an object’s property?
when accessing keys that have numbers, spaces or special characters
are objects mutable or immutable?
mutable
when the data stored on an object is a function, that is called a __
method
a property is what an object ___
a method is what an object ___
has
does
how are object methods invoked?
by appending the object’s name with the dot operator followed by the method name and parentheses
key.method( );
what does it mean that objects are passed by reference?
when we pass a variable assigned to an object into a function as an argument, the computer interprets the parameter name as pointing to the space in memory holding that object
what is the result of objects being passed by reference?
functions which change object properties mutate the object permanently - even if the object is assigned to to a const variable
what does the for… in syntax do?
will execute a given block of code for each property in an object
are arrays objects?
yes
what does the this keyword do?
references the calling object which provides access to the calling object’s properties
for a method, the calling object is the object the method ___ to
belongs to
what happens if you use the this keyword in a method?
the value of this becomes to calling object
arrow functions inherently __ an already defined __ value to the function itself that is not the calling object
bind
this
should you use arrow functions when using this in a method?
no! avoid!
what is privacy in objects?
the idea that only certain object properties should be mutable or able to change in value
does JavaScript have built in privacy for objects?
no
what is the naming convention for indicating that an object property should not be altered?
placing an underscore at the start of the property name (should prepend the property name)
_propertyName
what are getters?
getters are methods that get and return the internal properties of an object
what do setter methods do?
safely reassign values of existing properties within an object
in general, do getters need to be called with parentheses?
no
what is a factory function?
a function that returns an object and can be used to make multiple object instances
what is destructuring?
shortcuts for assigning properties to variables
how do you create destructured assignment with objects?
create a variable with the name of an object’s key wrapped in curly brackets and assign it to the object
const { object_key } = ___
what is called every time a new instance of a class is created?
the constructor( ) method
what is the difference between class and object syntax?
the constructor method (for classes)
what is an instance?
an object that contains property names and methods of class, but with unique property values
how the syntax for class methods and getters different from object syntax for methods and getters?
you can’t use commas between class methods
is the syntax for calling methods and getters on an instance the same as calling them on an object?
yes
what is a parent class also known as?
a superclass
what are child classes also known as?
subclasses
when are classes candidates for inheritance?
when multiple classes share properties or methods
what does the super keyword do?
calls the constructor of the parent class
how do you avoid reference errors when creating classes using inheritance?
always call the super keyword method before using the this keyword
where is the super keyword placed in best practice?
on the first line of subclass constructors
what happens when extends is called on a class declaration?
all of the parent methods are available to that child class
can child classes have their own properties, getters, setters, and methods outside of those they inherit?
yes
what are static methods?
methods that aren’t available in individual instances, but that can be called directly from the class
you cannot call static methods on a ___
instance
how do you define a class?
using a class declaration
in what HTML element is JavaScript encapsulated in?
< script >
what happens when an HTML parser comes across JavaScript
it stops to load the JS content before parsing the rest of the HTML
what are web events?
user interactions and browser manipulations that you can program to trigger functionality
what is the returned value of a function without a return statement?
undefined
what is a queue in computer science?
an abstract data structure where items are kept in order
what do comparison operators return?
a boolean true or false
what is the difference between === and ==
=== is strict equality == is the equality operator
if values being compared by a strict equality operator have different types, they are considered ___
unequal (will return false)
what happens when values being compared by the equality operator (==) are not the same type?
the equality operator performs a type conversion and the evaluates the values
what is this !== operator called
the strict inequality operator
what is this operator: | |
the logical or operator
what does the logical or operator ( ||) do?
returns true if either of the operands is true, otherwise it returns false
what do switch statements do?
they test a value and can have many case statements which define various possible values - statements are executed from the first matched case value until a break is encountered
how are case values tested in a switch statement?
with strict equality (===)
what does a default statement within a switch statement do?
the default statement is executed if no matching case statements are fond
where should the default statement go in a switch statement?
at the end (like an else statement)
what happens when a return statement is reached within a function?
the execution of the current function stops
how do you separate items within an object
with a comma ,
how many instances of a class does querySelector return?
just one
What would you use if you wanted to select all instances of something in DOM?
querySelectorAll
What is nodeList?
collections of nodes
what is a node?
a node is an abstract base class upon which many other DOM API objects are based - letting those object types to be used similarly and often interchangeably
what happens if you set setTimeout to 0
it still causes the event to be asynchronous