JavaScript Flashcards
What is the purpose of variables
Used to store (remember) data; short-term memory
How do you declare a variable?
Variable keyword and variable identifier (name)
What characters are allowed in variable names?
Numbers and letters, $, and underscores; numbers cannot be the first character
What does it mean to say that variable names are case sensitive?
Casing all needs to be the same or they are not the same
What is the purpose of a string
Store and manipulate text
What is the purpose of a number
To contain numeric data for calculations
What is the purpose of a Boolean
Represents true or false (binary values) useful for decision making
What does the = operator mean in JavaScript
Assignment operator; assigns a value to a variable
How do you update the value of a variable
Put the variable name again and using the assignment operator you just change the value
What is the difference between null and undefined
Null: represents nothing, something that doesn’t exist (intentional nothing put by the programmer)
Undefined: something isn’t there
Why is it good habit to include “labels” when you log values to the browser console
It’s more clear to see what data is being logged to the console and is a helpful debugging tool
Give five examples of JavaScript primitives
String, number, Boolean, undefined, null
What data type is returned by an arithmetic operation
Number
What is string concatenation
Two or more strings joined together to make a single string
What purpose(s) does the + plus operator serve in JavaScript
Addition or concatenation
What data type is returned by comparing two values
Boolean
What does the += “plus-equals” operator do
Addition assignment; adds a value then updates the variable
What are objects used for
Used to group together sets of variables and functions that have some relation to each other (can include functionality) to model or represent something
What are object properties
They are variables used within an object
Describe object literal notation
Curtly braces: sets of key-value pairs (property or method and it’s value separated by a colon) and the key-value pairs which are separated by commas except for the last one
How do you remove a property from an object
Delete keyword followed by name of the object, member operator (.), and name of property or method you want to delete
What are two ways to get or update the value of a property
Dot notation: object.property/method
Bracket notation: object[property/method]
What are arrays used for
Storing lists of values related to each other
Describe array literal notation
Data separated by commas within square brackets
How are arrays different from plain objects
Arrays have an order and their values are paired with their index (numeric index)
Objects have set properties (alpha-numeric indices) and are just collections of data with no set order
What number represents the first index of an array
0
What is the length property of an array
Array.length
Amount of items in an array
How do you calculate the last index of an array
Array.length - 1
What is a function in Javascript
A repeatable block of code designed to perform a specific task when it is invoked
Describe the parts of a function definition
Function keyword, optional function name, comma-separated parameter list of 0 or more parameters, open curly brace for the code block, optional return statement (return keyword followed by optional value), closing curly brace
When comparing them side-by-side, what are the differences between a function call and a function definition
The function call does not have the function keyword or code block within the curly braces
What is the difference between a parameter and an argument
Parameters are placeholders are variables that we declare in the function definition and that have an unknown value until we call the function and pass in the argument(a)
Arguments are actual values that we pass in when calling the function
Why are function parameters useful?
Allows the function to be reusable
Why do we log things to the console?
to see what our code is doing, if our code is functioning properly, debugging tool
What is a method?
a function which is a property of an object
How is a method different from any other function?
methods are attached to objects; you need to say specifically where the method is coming from
How do you remove the last element from an array?
.pop() method
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
gives you a decimal between 0 and 1 not including 1
How do you delete an element from an array?
.splice(index, #to delete, optional replacement)
How do you append an element to an array?
.push() and .unshift()
How do you break a string up into an array?
.split(separator)
Do string methods change the original string? How would you check if you weren’t sure?
no, because strings are immutable, but you can use the console log to check
Roughly how many string methods are there according to the MDN Web docs?
~35
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?
booleans
What is the purpose of an if statement?
to make decisions, to tell the code what to do in certain situations
Is else required in order to use an if statement?
nope
Describe the syntax (structure) of an if statement.
if (condition) {
code block;
}
What are the three logical operators?
&& (and), || (or), ! (not, bang)
How do you compare two different expressions in the same condition?
using a logical operator (&& or ||) between the two expressions
What is the purpose of a loop?
repeatable code
What is the purpose of a condition expression in a loop?
to drive the loop to terminate
What does “iteration” mean in the context of loops?
each individual time the code block runs
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?
BEFORE ANYTHING; evaluated once
When does the condition expression of a for loop get evaluated?
after the initialization the first time and then happens every time BEFORE EACH
When does the final expression of a for loop get evaluated?
the very end of the loop, after the statement is executed
AFTER EACH
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?
increments by 1, changes the value of the 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 does the event.preventDefault() method do?
it prevents the page from reloading when the form is submitted
What does the event.preventDefault() method do?
it prevents the default (if there is one) from happening
What does submitting a form without event.preventDefault() do?
it will reload the page and the data will be attached to the url (default of form is to send the information of that form to the url to store)
What property of form a control object gets and sets its value?
value
What property of a form control object gets and sets its value?
value
What is an advantage of having your console open when writing a JavaScript program?
you can see what is happening as you work
What is an advantage of having your console open when writing a JavaScript program?
you can see what is happening as you work
Does the document.createElement() method insert a new element into the page?
no, we are just creating the element
How do you add an element as a child to another element?
parent.appendChild(child)
What do you pass as the arguments to the element.setAttribute() method?
name, value
What steps do you need to take in order to insert a new element into the page?
createElement, add any additional info to it, append it to the parent (assuming that parent is already part of the page)
What is the textContent property of an element object for?
to get, set, or update text withing that textNode
Name two ways to set the class attribute of a DOM element.
setAttribute
className
classList
(be consistent)
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
- allows it to be repeatable
- you can give a function a name that makes it easily understandable when reading code
- more efficient
Give two examples of media features that you can query in an @media rule.
aspect ratio
height (of viewport)
What is the event.target?
it’s the element that the event is firing on; holds the dom element that you most directly interacted with
What is the effect of setting an element to display: none?
the element won’t appear on the page; acts as if the element doesn’t exist (removes the element from the document flow)
What does the element.matches( ) method take as an argument and what does it return?
argument: a string of the css selector you want to select
return: boolean true or false
How can you retrieve the value of an element’s attribute?
getAttribute
At what steps of the solution would it be helpful to log things to the console?
after every step
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
you would have to put an event listener on every single tab
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
if statement for each tag
What is JSON?
(JavaScript Object Notation)
a text-based data format following JavaScript object syntax and it can be used independently from JS
What are serialization and deserialization?
serialization: converting a native object to a string so it can be transmitted across the network
deserialization: Converting a string to a native object
Why are serialization and deserialization useful?
for data exchange and storing data without having to rely on it storing as a reference in your computer
it allows you to save the state of an object and convert it to 1s and 0s so it can be transferred over networks and then it can be translated back into an object through deserialization
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( ) method
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( ) method
How to you store data in localStorage?
.setItem of the localStorage item allows us to store in localStorage
How to you retrieve data from localStorage?
.getItem
What data type can localStorage save in the browser?
string values
When does the ‘beforeunload’ event fire on the window object?
happens before the page will no longer be loaded (closing a tab, closing a browser window, navigating to a new window)
What is a method?
a function which is a property of an object
How can you tell the difference between a method definition and a method call?
method definition has a code block and function keyword whereas method call doesn’t have the code block and you would pass in arguments if necessary
Describe method definition syntax (structure).
has a function keyword, optional function name followed by a comma-separated list of 0 or more parameters as well as curly braces for the code block
Describe method call syntax (structure).
the object name, member operator, the name of the function followed by a comma-separated list of 0 or more arguments in parentheses
How is a method different from any other function?
methods must be attached to an object, you must specify where that method is coming from
** you can reference data within the function
What is this in JavaScript?
“the object to the left of the dot” when the function is called (as a method).
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object
** if you cannot see the function being called, then you do not know what the value of this will be
What does it mean to say that this is an “implicit parameter”?
it is available in a function’s code block even though it was never included in the function’s parameter list or declared with var.
When is the value of this determined in a function; call time or definition time?
call time
What does ‘this’ refer to in the following code snippet?
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
don’t know until call time
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
“it’s-a-me, Mario!”
why:
What is this in JavaScript?
A keyword that refers to the object it belongs to
(“the object to the left of the dot” when the function is called (as a method).
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object
** if you cannot see the function being called, then you do not know what the value of this will be)
How can you tell what the value of this will be for a particular function or method definition?
you can’t tell until the method or function is called
How can you tell what the value of this is for a particular function or method call?
look at the object left of the dot when the function or method is being called
What does ‘this’ refer to in the following code snippet?
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
its nothing because there is nothing using this
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
“it’s-a-me, Mario!”
why: because this is referring to the character object and getting the firstName property out of that object which is “Mario”; we know it’s character because it’s left to the dot
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet; hello();
“it’s-a-me, undefined!”
why: there’s no reference to the value of the variable this. if there’s no dot at call time, the default is “window”
you’re no longer calling that function on the object that includes the this keyword so it’s undefined
How can you tell what the value of this is for a particular function or method call?
look at the object left of the dot when the function or method is being called
What kind of inheritance does the JavaScript programming language use?
prototypal inheritance
What is a prototype in JavaScript?
an object that contains properties and (predominantly) methods that can be used by other objects
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
because of prototypal inheritance, they borrow the prototype object and its methods
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
because of prototypal inheritance, they borrow the prototype object and its methods
*using methods on the prototype object
What does the new operator do?
- creates a blank plain javascript object
- adds a property to the new object that links it to the prototype
- binds the new object instance as the “this” context
- returns this if the function doesn’t return an object
What does the new operator do?
creates an instance (1 iteration) of a type of thing
- creates a blank plain javascript object
- adds a property to the new object that links it to the prototype
- binds the new object instance as the “this” context
- returns this if the function doesn’t return an object
What does the instanceof operator do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object
What does the instanceof operator do?
checks if the object on the right of the operator is a prototype anywhere in the chain
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object
returns a boolean value
What is a “callback” function?
a function passed into another function as data (argument) which is then invoked inside the outer function to perform a specific task
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setTimeout( ) function
How can you set up a function to be called repeatedly without using a loop?
setInterval( ) function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
timerId
the delay time, a positive integer value identifying the timer created by the call to setTimeout( ) or setInterval( )
What is AJAX?
Ajax allows you to request data from a server and load it without having to refresh the entire page
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest object
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load events
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
inheritance possibly?