JavaScript Flashcards
What is the purpose of variables?
store date for later use with a name
How do you declare a variable?
keyword var
How do you initialize (assign a value to) a variable?
= sign
What characters are allowed in variable names?
_$@#1 and letters, cant start with #
What does it mean to say that variable names are “case sensitive”?
needs consistency with uppercase/lowercase
What is the purpose of a string?
hold letters/characters
What is the purpose of a number?
hold numeric values
What is the purpose of a boolean?
logical value true/false
What does the = operator mean in JavaScript?
assign value
How do you update the value of a variable?
assign it a new value
What is the difference between null and undefined?
null is intentional and is assigned/ undefined is not intentional and is not assigned
Why is it a good habit to include “labels” when you log values to the browser console?
so you know what output is for what console log
Give five examples of JavaScript primitives.
string, number, boolean, null, undefined, symbol, bigint
What data type is returned by an arithmetic operation?
number
What data type is returned by an arithmetic operation?
number
What is string concatenation?
joining/combining 2 strings together for a new string
What purpose(s) does the + plus operator serve in JavaScript?
adding 2 values or concatenating
What data type is returned by comparing two values (, ===, etc)?
boolean T/F
What does the += “plus-equals” operator do?
addition assignment, adds additional info to variable to add to
What are objects used for?
grouping together variables and functions
What are object properties?
additional information for the object, name/value, individual keys in an objects
Describe object literal notation.
var object = { # = 1 } declare variable and assign property/value
How do you remove a property from an object?
using delete operator then dot notation for object.property
What are the two ways to get or update the value of a property?
bracket notation, dot notation
What are arrays used for?
store a list of values that are related
Describe array literal notation.
var name = [ ]
How are arrays different from “plain” objects?
hold different amounts of information,
arrays don’t need individual keys
arrays have length property and important to know amount
object not important to know number
What number represents the first index of an array?
[0]
What is the length property of an array?
number of items in an array
How do you calculate the last index of an array?
using the length property then subtracting one
What is a function in JavaScript?
a set of statements to perform tasks with related inputs and outputs and reuseable
are objects
Describe the parts of a function definition.
The function keyword to begin the creation of a new function.
An optional name.
A comma-separated list of zero or more parameters, surrounded by () parentheses.
The start of the function’s code block, as indicated by an { opening curly brace.
An optional return statement.
The end of the function’s code block, as indicated by a } closing curly brace.
a set of inputs, a set of outputs, and a rule that relates the elements
Describe the parts of a function call.
The function’s name.
A comma-separated list of zero or more arguments surrounded by () parentheses.
Our sayHello function does not take any arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
definition doesn't return anything but calling the function does code block function keyword
What is the difference between a parameter and an argument?
parameter is a placeholder
argument is the value
recipe vs ingredients
Why are function parameters useful?
to give the function values, varying results
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a value we can use in our program.
Prevents any more code in the function’s code block from being run.
Why do we log things to the console?
to check the result of the code and to make sure everything is working
What is a method?
In JavaScript functions themselves are objects, so, in that context, a method is actually an object reference to a function. A method is a function which is a property of an object. function stored in a property
How is a method different from any other function?
a method is associated with an object property, while a function is not.
How do you remove the last element from an array?
pop( )
How do you round a number down to the nearest integer?
math. floor ( )
How do you generate a random number?
math.random
random method
How do you delete an element from an array?
splice ( )
splice(start, deleteCount)
How do you append an element to an array?
push–end
unshift-beginner
How do you break a string up into an array?
split ( )
Do string methods change the original string? How would you check if you weren’t sure?
no, you can log it
strings are immutable
Roughly how many string methods are there according to the MDN Web docs?
over 40
Is the return value of a function or method useful in every situation?
no if there are additional methods/functions after
Roughly how many array methods are there according to the MDN Web docs?
over 40
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
=, ===, !=, >=, <=, ==
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
evaluates a condition
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword
( ) arguments
{ } code block
What are the three logical operators?
&& logical and
|| or
! not
How do you compare two different expressions in the same condition?
use the logical operator
What is the purpose of a loop?
a sequence of instructions that is continually repeated until a certain condition is met in computer programming
repeat a block of code automatically
What is the purpose of a condition expression in a loop?
to see if the loop should run
What does “iteration” mean in the context of loops?
a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met.
set of expressions
Means how many times the loop will loop.
When does the condition expression of a while loop get evaluated?
before each iteration
When does the initialization expression of a for loop get evaluated?
once before the loop begins
When does the condition expression of a for loop get evaluated?
before the iteration after initialization
When does the final expression of a for loop get evaluated?
at the end of every iteration
before the next evaluation of condition
after code block runs
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
adds one to the operand
and reassigns
substitutes value
How do you iterate through the keys of an object?
for … in loop statement
What object is passed into an event listener callback when the event fires?
the target object/element
Why do we log things to the console?
check functionality and remove potential bugs
What is a “model”?
representation of something
Which “document” is being referred to in the phrase Document Object Model?
the HTML document
What is the word “object” referring to in the phrase Document Object Model?
the elements
the data type object
What is a DOM Tree?
a model of the webpage
connects parent elements and children nodes
Give two examples of document methods that retrieve a single element from the DOM.
get elements by id
*query selector
Give one example of a document method that retrieves multiple elements from the DOM at once.
query selector all
Why might you want to assign the return value of a DOM query to a variable?
to make it easier for the browser to look for
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
directory
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
Because the browser needs to go through all the HTML elements before the JavaScript code can access it.
What does document.querySelector() take as its argument and what does it return?
the css selector
* The first element within the document that matches the selector or null
What does document.querySelectorAll() take as its argument and what does it return?
A css selector (universal, type, class, id or attribute) and it returns a Node List with all the properties of that selector.
Why do we log things to the console?
to check functionality
What is the purpose of events and event handling?
for user interactions
Are all possible parameters required to use a JavaScript method or function?
no, there are functions with no parameters
optional parameters
What method of element objects lets you set up a function to be called when a specific type of event occurs?
add event listener ( )
What is a callback function?
A function that takes another function as parameter and is not called by us. function definition being passed around with value
What object is passed into an event listener callback when the event fires?
the event object
all the data of the event that occurred
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It is the elements that was interacted with. MDN.
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick( ))
first is a callback function
second is just a separate function
What is the className property of element objects?
Property of DOM element that gets and sets the className of an element.
How do you update the CSS class attribute of an element using JavaScript?
using the class name var cName = elementNodeReference.className; elementNodeReference.className = cName;
What is the textContent property of element objects?
Property that gets and sets the text content of an element and child
How do you update the CSS class attribute of an element using JavaScript?
query the class
How do you update the text within an element using JavaScript?
query the element
text content property
Is the event parameter of an event listener callback always useful?
no, it is not always used
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
a lot more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
Because storing DOM locations in a variable prevents the browser from looking for the information.
easier for javascript to work with
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a ?
submit
What does the event.preventDefault() method do?
It prevents the default event functionality from being fired from happening.
What does submitting a form without event.preventDefault() do?
automatically reloads the form
What property of a form element object contains all of the form’s controls.
.elements property
What property of a form control object gets and sets its value?
.value property
What is one risk of writing a lot of code without checking to see if it works so far?
there could be bugs that could have been prevented
What is an advantage of having your console open when writing a JavaScript program?
debugging
seeing events
Does the document.createElement( ) method insert a new element into the page?
no
How do you add an element as a child to another element?
. appendChild ( )
What do you pass as the arguments to the element.setAttribute( ) method?
the attribute name and attribute value