JavaScript Flashcards
What is the purpose of variables?
to store data and information to be used later. keeps a history of data
How do you declare a variable?
var
let
const
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
letters, $, _, numbers(numbers cannot be the first character)
What does it mean to say that variable names are “case sensitive”?
casing matters in variables
What is the purpose of a string?
to be able to give a variable a value of text
What is the purpose of a number?
to assign variables a numerical value
What is the purpose of a boolean?
making decisions
What does the = operator mean in JavaScript?
assigns a value to something so that it will contain it
How do you update the value of a variable?
declare and assign it a new value
What is the difference between null and undefined?
null is an empty value purposely given while undefined means nothing is defined yet
Why is it a good habit to include “labels” when you log values to the browser console?
specifies what the value relates to
Give five examples of JavaScript primitives.
boolean string number null undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
appending a string to another end of a string
What purpose(s) does the + plus operator serve in JavaScript?
string concatenation and addition
What data type is returned by comparing two values (< , > , ===, etc)?
boolean
What does the += “plus-equals” operator do?
addition assignment and the result of that will be the updated value
What are objects used for?
to store multiple types of data
What are object properties?
a piece of data stored in an object
Describe object literal notation.
data within a { and a , separating each one
How do you remove a property from an object?
delete object.property
What are the two ways to get or update the value of a property?
dot notations and square brackets
What are arrays used for?
objects with similar data types in a list format
Describe array literal notation.
declare a variable
square bracket
values contained in the bracket
How are arrays different from “plain” objects?
arrays are ordered, indexed numerically, constant count of what is inside
What number represents the first index of an array?
0
What is the length property of an array?
number of values in the array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
reusable block of code
Describe the parts of a function definition.
function keyword name of function parameter(s) code block return keyword
Describe the parts of a function call.
( )
When comparing them side-by-side, what are the differences between a function call and a function definition?
function calls does not have the keyword function function calls have arguments and data
What is the difference between a parameter and an argument?
parameter is the place holder which is used when defining a function
arguments replace the parameter when the function is being called
Why are function parameters useful?
so that you can provide values with arguments when the function is being called
What two effects does a return statement have on the behavior of a function?
produces a value from the function
it stops the code of the function from running any further
Why do we log things to the console?
verify outputs. a debugging mechanism
What is a method?
function stored in a property
How is a method different from any other function?
methods are attached to objects
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()
gives a number from 0-1. inclusive of 0 but not of 1.
How do you delete an element from an array?
splice()
How do you append an element to an array?
push()
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
console.log() to check
Is the return value of a function or method useful in every situation?
No. You may be using the function or method for what it does and not necessarily the return
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?
booleans
What is the purpose of an if statement?
to determine which code block is to be run
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, conditional code block
if (condition) {
code block
}
What are the three logical operators?
&& , || , !
How do you compare two different expressions in the same condition?
&& , ||
Why do we log things to the console?
to make sure our code is correct
What is a “model”?
something that is easier to represent of a concept.
a 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?
referring to javascript objects
What is a DOM Tree?
collection of html elements similar to a family tree
Give two examples of document methods that retrieve a single element from the DOM.
querySelectorAll
querySelector
(use these methods only)
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
to store the data and use it later
What console method allows you to inspect the properties of a DOM element object?
dir method
dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
because it is read top to bottom. if you put the script tag above anything, it won’t be read
What does document.querySelector() take as its argument and what does it return?
css selector as an argument in a string form.
returns one object
finds the first selector and disregards the rest
What does document.querySelectorAll() take as its argument and what does it return?
css selector as an argument in a string form.
returns an object
finds all selectors and produces it
Why do we log things to the console?
to check if your code works
What is the purpose of events and event handling?
is for user interaction
Are all possible parameters required to use a JavaScript method or function?
no
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function?
a function that is passed inside another function as an argument
What object is passed into an event listener callback when the event fires?
it is the object javascript created when the event happens which is passed into the call back function
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
object referencing where ever this event started from
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
first one has a callback function
second one has a function call (second one, the event will happen immediately when the page loads. event listener will not work)
What is the className property of element objects?
sets the name of a class name
How do you update the CSS class attribute of an element using JavaScript?
object.className = “new class name”
What is the textContent property of element objects?
allows you to update the text of the object
How do you update the text within an element using JavaScript?
.textContent on a dom object with an assignment operator to update the text
object.textContent = “text content”
Is the event parameter of an event listener callback always useful?
no
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
its more easily accessible in a variable
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 form?
submit
What does the event.preventDefault() method do?
it stops the default behavior of something
example: putting event.preventDefault() within a function in an event listener with submit buttons will make sure the page does not refresh when you click submit