JavaScript Flashcards
What is the purpose of variables?
to store data/information
without variables, we wouldn’t know what information would pertain to what
see data that come together from the past
and preserve data for the future
How do you declare a variable?
var variable;
var is the “variable keyword”
and “variable” is the variable name
How do you initialize (assign a value to) a variable?
variable name = value (number boolean string null undefined);
What characters are allowed in variable names?
letters numbers underscores and dollar signs
What does it mean to say that variable names are “case sensitive”?
capitals and lower cases of the same letter are completely different
What is the purpose of a string?
give variables value of text
What is the purpose of a number?
hold numerical values we use it for math, predominately
What is the purpose of a boolean?
to compare values with true or false
booleans are for making decisions
What does the = operator mean in JavaScript?
The equals sign (=) is an assignment operator. It is how you
assign a value to the variable. The assignment operator makes it so variables will contain a value
How do you update the value of a variable?
variable = new value;
What is the difference between null and undefined?
null is intentionally empty
undefined means theres no value there (has not been assigned a value yet)
Why is it a good habit to include “labels” when you log values to the browser console?
immediate identifier
good for a reference point
Give five examples of JavaScript primitives.
number boolean string null undefined
What data type is returned by an arithmetic operation?
number data type
What is string concatenation?
appending one string to the end of another string
What purpose(s) does the + plus operator serve in JavaScript?
addition
concatenating strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
The plus-equals operator ( += ) adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left.
What are objects used for?
to group together properties or data
What are object properties?
chunk of data attached to one of those pieces of information
Describe object literal notation.
inside curly braces properties: values, each property separated by commas
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 notation and square brackets
What are arrays used for?
to store items with similar data types with a similar purpose; similar values in a list format. arrays are lists
Describe array literal notation.
[item, item, item]
How are arrays different from “plain” objects?
arrays are ordered, are indexed numerically, and keep a constant count of what’s inside it
What number represents the first index of an array?
0
What is the length property of an array?
number of values within the array; true count (starts its count at 1)
How do you calculate the last index of an array?
the value of the length’s property - 1
array.length - 1
What is a function in JavaScript?
reusable block of code
Describe the parts of a function definition.
declaration using the word “function” (function keyword)
function name (parameter) { // do something return something }
Describe the parts of a function call.
functionName(argument)
When comparing them side-by-side, what are the differences between a function call and a function definition?
no function keyword on a function call, no function block in a function call either
What is the difference between a parameter and an argument?
parameter is a placeholder. and an argument is the actual data that fills those parameters when we actually use the function
Why are function parameters useful?
you are able to provide values to the function and you can put different values into the same function
What two effects does a return statement have on the behavior of a function?
produces a value and stops the function entirely
return statement ends the function
Why do we log things to the console?
verifiy youre getting the right output
debugging mechanism
What is a method?
a function stored in a property of an object
Source: Lecture
How is a method different from any other function?
methods are attached to objects. functions are 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()
How do you delete an element from an array?
.splice(indexWhereToStart, howManyElementsToDelete)
How do you append an element to an array?
.push(element);
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 would check if you weren’t sure with console logging
Is the return value of a function or method useful in every situation?
ex .pop() maybe we weren’t concerned with what was being deleted; only that something was deleted
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN (our trusted source)
Give 6 examples of comparison operators.
==, !=, >, =, <=,===
Source: textbook duckett pages 150 - 151
What data type do comparison expressions evaluate to?
boolean
Answer from Textbook Page 151: “[Conditions] usually result in a value of true or false.”
What is the purpose of an if statement?
to check or evaluate if a condition is true or false; to make a decision.. should I do this or not.
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if keyword ( condition ) { }
What are the three logical operators?
&&, ||, !
logical and, logical or, and logical not
Source: Textbook Page 157
How do you compare two different expressions in the same condition?
Using logical operators
Source: Textbook Page 156
What is the purpose of a loop?
to keep running a block of code until the condition is no longer met
a purpose of a loop is to be a way to automate actions that need to be repeated
What is the purpose of a condition expression in a loop?
condition expression is where it ends
What does “iteration” mean in the context of loops?
one execution of the loop block
When does the condition expression of a while loop get evaluated?
before the code block executes
When does the initialization expression of a for loop get evaluated?
before the loop begins
very first action that happens before anything
When does the condition expression of a for loop get evaluated?
before the code block
just to double check if we should be doing anything
When does the final expression of a for loop get evaluated?
after the code block gets executed
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 1 to the variable
How do you iterate through the keys of an object?
for..in loop
Why do we log things to the console?
to check to see if everything is working
What is a “model”?
a recreation of somthing; not the real thing
Which “document” is being referred to in the phrase Document Object Model?
html (core of everything; first thing you see when opening a webpage)
What is the word “object” referring to in the phrase Document Object Model?
datatype
What is a DOM Tree?
layout similar to a family tree that shows relations to each other
Give two examples of document methods that retrieve a single element from the DOM.
getElementById
querySelector
Give two examples of document methods that retrieve a single element from the DOM.
querySelector() for single elements
querySelectorAll() for multiple
Why might you want to assign the return value of a DOM query to a variable?
to allow the browser to find the element faster
What console method allows you to inspect the properties of a DOM element object?
dir method
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
so that the html loads first
because the browser reads the code from top to bottom
What does document.querySelector() take as its argument and what does it return?
a) a datatype DOM string
b) DOM element object
What does document.querySelectorAll() take as its argument and what does it return?
a) everything in a node list b) node.list
What is the purpose of events and event handling?
user interaction
Are all possible parameters required to use a JavaScript method or function?
No. Example: .addEventListener()
Duckett has a boolean argument and MDN does not
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?
functions passed inside another function as an argument
What object is passed into an event listener callback when the event fires?
event object (object built by JavaScript engine that relays al the information of the event)
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The read-only target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.
Console.log
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
1) is the callback function
2) is the return of the function call (got called before it could actually run)
What is the className property of element objects?
How do you update the CSS class attribute of an element using JavaScript?
use .className
update by assign value
What is the textContent property of element objects?
update the text content of the object
How do you update the text within an element using JavaScript?
.textContent property of the DOM object
Is the event parameter of an event listener callback always useful?
No. Example: Did not make use of the event object at all
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, because then we would have to query the DOM to keep track of the number of clicks, which would be a pain
Why is storing information about a program in variables better than only storing it in the DOM?
optimizes the process
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