javascript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
A variable keyword and then the variable name
How do you initialize (assign a value to) a variable?
Using the assignment operator (=)
What characters are allowed in variable names?
Letters, numbers, dollar sign, or an underscore
What does it mean to say that variable names are “case sensitive”?
You can reuse the same name with different capitalization
What is the purpose of a string?
Holding data in text form
What is the purpose of a number?
For tasks involving counting or calculating and to store numerical data
What is the purpose of a boolean?
To store data as either a yes or no (lightswitch); yes it should do this or no it should not
What does the = operator mean in JavaScript?
Assignment operator; Assign a value to a variable
How do you update the value of a variable?
Using the assignment operator
What is the difference between null and undefined?
Undefined is a type while null is an object; Undefined is a variable that has been declared but the value has not
Why is it a good habit to include “labels” when you log values to the browser console?
To help keep track of what you are console logging
Give five examples of JavaScript primitives.
String, number, undefined, null, boolean
What data type is returned by an arithmetic operation?
Numbers
What is string concatenation?
Combining two strings
What purpose(s) does the + plus operator serve in JavaScript?
To add numbers and concatenate strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Concatenates and assigns the new value to the original variable
What are objects used for?
To store properties and their values. They are meant to model real world objects.
What are object properties?
They tell us about the object (such as the name of a hotel)
Describe object literal notation.
You create an object by defining the key value pairs and assign it to a variable var student = { name = Shawn }
How do you remove a property from an object?
Using the delete operator
What are the two ways to get or update the value of a property?
Dot notation & Bracket notation
What are arrays used for?
Lists
Describe array literal notation.
Using the square brackets
How are arrays different from “plain” objects?
Arrays are indexed numerically
What number represents the first index of an array?
0
What is the length property of an array?
Shows you numerically the length of the array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
Combining multiple lines of code into a function for ease of access and readability
Describe the parts of a function definition.
The function keyword along with an optional name and a parameter surrounded by parenthesis and then an opening and closing curly brace
Describe the parts of a function call.
The functions name with an argument surrounded by parenthesis
When comparing them side-by-side, what are the differences between a function call and a function definition?
The parameter becomes an argument and you don’t need the curly braces
What is the difference between a parameter and an argument?
A parameter is defined with the function, while an argument is passes when calling a function
Why are function parameters useful?
To store and pass data and provide extra information about the function
What two effects does a return statement have on the behavior of a function?
Causes the code block within the function to be ran, and prevents more code from being run.
Why do we log things to the console?
So we can see what’s actually happening with our code.
What is a method?
A method is a function which is a property of an object.
How is a method different from any other function?
A method is associated with an object
How do you remove the last element from an array?
Pop method
How do you round a number down to the nearest integer?
Round method
How do you generate a random number?
Math.random method
How do you delete an element from an array?
Splice method
How do you append an element to an array?
Push method
How do you break a string up into an array?
Split method
Do string methods change the original string? How would you check if you weren’t sure?
No; console log
Roughly how many string methods are there according to the MDN Web docs?
50
Is the return value of a function or method useful in every situation?
No
Roughly how many array methods are there according to the MDN Web docs?
37
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?
To make decisions on which code to run
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
Keyword, then condition, then opening curly brace, the code you want to run, and then a closing curly brace.
What are the three logical operators?
&&; ||; ! (and, or, logical not)
How do you compare two different expressions in the same condition?
Logical operators
What is the purpose of a loop?
To keep checking conditions until a statement is true.
What is the purpose of a condition expression in a loop?
Setting how long the loop should run
What does “iteration” mean in the context of loops?
Each time a loop runs
When does the condition expression of a while loop get evaluated?
Before the statement is executed
When does the initialization expression of a for loop get evaluated?
Before the condition
When does the condition expression of a for loop get evaluated?
After the initialization and before the final-expression
When does the final expression of a for loop get evaluated?
After the condition
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?
Increase by 1
How do you iterate through the keys of an object?
For-in loops
Why do we log things to the console?
So we can see what’s actually happening.
What is a “model”?
A recreation of something
Which “document” is being referred to in the phrase Document Object Model?
The current html document you are working on
What is the word “object” referring to in the phrase Document Object Model?
Represents the different objects within the DOM
What is a DOM Tree?
A model of a web page
Give two examples of document methods that retrieve a single element from the DOM.
getElementById() and querySelector()
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 location of the element. This way the DOM doesn’t have to search through the DOM Tree.
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
It needs to be loaded after all the HTML content
What does document.querySelector() take as its argument and what does it return?
It takes a css selector and returns only the first of the matching elements
What does document.querySelectorAll() take as its argument and what does it return?
It takes a css selector and returns all elements that match
Why do we log things to the console?
To see what data is being passed
What is the purpose of events and event handling?
To run code based off of things that happen on the web page
What do [] square brackets mean in function and method syntax documentation?
It means the argument is optional
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 passed into another function as an argument
What object is passed into an event listener callback when the event fires?
The event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The place where the event is being invoked, and in the debugger.
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The second one will run as soon as it hits the line of code, instead of when the event occurs
What is the className property of element objects?
the string within the class attribute
How do you update the CSS class attribute of an element using JavaScript?
Using the className property
What is the textContent property of element objects?
the text content of the node and its decendants
Is the event parameter of an event listener callback always useful?
Useful but not necessary
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
Harder
Why is storing information about a program in variables better than only storing it in the DOM?
So you can access it again quickly and so you don’t depend on the DOM.
How do you update the text within an element?
Select the element and use the textContent property
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?
Prevents the default event of whatever it’s in
What does submitting a form without event.preventDefault() do?
Reloads the page after you submit
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?
It makes it a lot harder to debug
What is an advantage of having your console open when writing a JavaScript program?
You can see what’s happening at all times
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() method
What do you pass as the arguments to the element.setAttribute() method?
The attribute name then the value
What steps do you need to take in order to insert a new element into the page?
Create the element, give it content, and add it to the dom
What is the textContent property of an element object for?
To change the text content of that element
Name two ways to set the class attribute of a DOM element.
setAttribute() and className() methods
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can reuse the function later & so you don’t repeat yourself
What is the event.target?
The element interacted with during an event
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event flow makes it possible. You are clicking on an element that is within another element (event bubbling)
What DOM element property tells you what type of element it is?
tagName