JavaScript Flashcards
What is the purpose of variables?
stores data value that can be changed and used later on
How do you declare a variable?
using var keyword.
How do you initialize (assign a value to) a variable?
var keyword, variable name, assignment operator, and value.
What characters are allowed in variable names?
variable names cannot contain any spaces. Must start with a letter, underscore, or dollar sign. names can only contain letters, numbers, underscores, or dollar signs. names are case sensitive.
What does it mean to say that variable names are “case sensitive”?
means any identifier must be typed with a consistent capitalization of numbers.
What is the difference between null and undefined?
null is representation of no value, undefined means a variable has been declared but not yet has been assigned a value.
Why is it a good habit to include “labels” when you log values to the browser console?
so that you know what is being logged.
Give five examples of JavaScript primitives.
strings, numbers, null, undefined, boolean
What data type is returned by an arithmetic operation?
numbers
What is string concatenation?
joining string together.
What purpose(s) does the + plus operator serve in JavaScript?
for concatenation and adding numeric values together
What data type is returned by comparing two values (< , >, ===, etc)?
boolean value
What does the += “plus-equals” operator do?
adds the value of the right operand to a variable and assigns the result to the variable.
What are objects used for?
objects group together a set of variable and functions to create a model of something.
What are object properties?
a variable that is part of an object.
Describe object literal notation.
var keyword, variable name, assignment operator, key value pairs.
How do you remove a property from an object?
use delete keyword followed by the object name and property name.
What are the two ways to get or update the value of a property?
object name, dot notation, followed by property name, assignment operator and property value. object name, square bracket, property name in string assignment operator, and property value.
What are arrays used for?
a list or set of values that are related to each other. Making lists of related of the exact same type.
What number represents the first index of an array?
0.
What is the length property of an array?
gives the number of items in array.
How do you calculate the last index of an array?
array.length - 1.
Describe array literal notation.
var keyword, variable name, assignment operator, square bracket, array values.
How are arrays different from “plain” objects?
arrays keys are index, object keys are properties.
What is a function in JavaScript?
a set of statements that performs a task or calculates a value and can be reused throughout a program
Describe the parts of a function definition.
function keyword, optional function name, parameter with optional values, followed by code block for the function. optional return statement.
Describe the parts of a function call.
function name, with optional arguments.
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call does not have the function keyword. function call does not have a code block to execute.
What is the difference between a parameter and an argument?
Parameters are variables listed as a part of the function definition. Arguments are values passed to the function when it is invoked.
Why are function parameters useful?
You can think of a parameter as a placeholder. It is basically a variable whose value is not known until we call the function and pass an argument. When the function’s code block is run, the parameter will be holding the value of the argument.
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?
debugging tool used to check what your output would be.
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?
methods are always related to objects, functions are not. methods are built in, functions are not. when using a method, there will always be a dot before the method and a parenthesis after.
How do you remove the last element from an array?
pop method on the array
How do you round a number down to the nearest integer?
using floor method of math object.
How do you generate a random number?
using random method of math object.
How do you delete an element from an array?
using pop method on the array to delete last item, or using shift method on the array to delete first item. You can also use splice method on the array and specify the beginning and number of items to manually delete items in array
How do you append an element to an array?
using push method on the array
How do you break a string up into an array?
using the split method on the string.
Do string methods change the original string? How would you check if you weren’t sure?
no, you can console log the original string to see if anything changed.
Roughly how many string methods are there according to the MDN Web docs?
about 50.
Is the return value of a function or method useful in every situation?
no because when using methods, the return value may not be needed.
Roughly how many array methods are there according to the MDN Web docs?
around 35
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.
strictly equals, less than, less than or equal to, greater than, greater than or equal to, not strictly equals.
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
a conditional statement in which if met, then your code executes one or more statements, else your code does something different. Decision making tool.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
if keyword, condition, and code block for if statement.
What are the three logical operators?
logical and (&&), logical or (||), logical not (!).
How do you compare two different expressions in the same condition?
using logical and (&&) or logical or (| |)
What is the purpose of a loop?
loops will run a set of code block until the condition evaluates to false. Used for repetition.
What is the purpose of a condition expression in a loop?
the loop runs until the counter reaches a specified number.
When does the condition expression of a while loop get evaluated?
An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed. When condition evaluates to false, execution continues with the statement after the while loop. Tells when to start and end.
What does “iteration” mean in the context of loops?
when the loop executes one round of the condition
When does the initialization expression of a for loop get evaluated?
once before the loop iteration begins, never again.
When does the condition expression of a for loop get evaluated?
An expression to be evaluated before each loop iteration. right after the final expression right before the code block.
When does the final expression of a for loop get evaluated?
An expression to be evaluated at the end of each loop iteration.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
use break statements to terminate a loop.
What does the ++ increment operator do?
adds one to its operand and returns a value
How do you iterate through the keys of an object?
using for in loop
What is the event.target?
show the element where an event happened.
What is the affect of setting an element to display none?
hides that element from viewport
What does the element.matches method take as an argument and what does it return?
css selector as a string as an argument and returns a boolean value of whether the elements match.
How can you retrieve the value of an element’s attribute?
Object.getAttribute method
At what steps of the solution would it be helpful to log things to the console?
every single 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 use add event listener on every new tabs. this is not scalable.
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?
you would have huge condition block for every view. it is not scalable.
What is JSON?
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. Data exchange format.
What are serialization and deserialization?
Converting a string to a native object is called deserialization, while converting a native object to a string so it can be transmitted across the network is called serialization.
Why are serialization and deserialization useful?
useful when you want to transmit data across a network.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse
How to you store data in localStorage?
setItem method on localStorage object with arguments of storage name in string and JSON variable
How do you retrieve data from localStorage?
getItem method on localStorage object with argument of storage name in string format.
What data type can localStorage save in the browser?
string
When does the ‘beforeunload’ event fire on the window object?
when the window, the document and its resources are about to be unloaded. before you close, refresh web page.
What is a method?
a set of procedures to follow in an object. A function which is a property of an object.
How can you tell the difference between a method definition and a method call?
method calls will always have an object and dot before the methods name followed by parenthesis. method definition is assigning function definition to object. method call will always have parenthesis.
Describe method definition syntax (structure).
inside an object literal variable, there is a key value pair. the key is the name of the method, and the value will be a function definition defining the method.
Describe method call syntax (structure).
object name, dot notation and method name followed by optional arguments.
How is a method different from any other function?
methods always related to objects, functions aren’t. functions can be called without objects.
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism.
What is “abstraction”?
simplifying complex steps of procedures into one generalized step so that it is more accessible to people who understand the steps. (ie. light switch).
What does API stand for?
application programming interface
What is the purpose of an API?
allows applications to communicate with each other. prewritten tools that developers can use
what is “this” in JavaScript?
the object that a property belongs to. object you are currently working with.
What does it mean to say that this is an “implicit parameter”?
parameter that is available in a function’s code block even though it was never included in the function’s parameter list or declared with var. Given to the function even though it is not listed as parameter.
When is the value of this determined in a function; call time or definition time?
call time. This does not have a value until function is called.
How can you tell what the value of this will be for a particular function or method definition?
you cant, value of this is defined when function or method is being called.
How can you tell what the value of this is for a particular function or method call?
Find where the function is called and look for an object to the left of the dot. If there is no value to the left of the dot, the value of this is window.
What does “this” refer to in the following code snippet?
the object character. nothing, because greet has not been invoked.
Given the code above “character” object, what is the result of the following code snippet and why?
It’s a me, Mario! Because we are calling the method of greet, which will log the message.
Given the code above “character” object, what is the result of the following code snippet and why?
undefined, because greet is a method, and character.greet will give us the function definition not the functionality of the function definition.
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance.
What is a prototype in JavaScript?
a JavaScript object that holds properties and methods that can later be used when needed. Object that other objects can build off of.
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?
grabbing it from prototypes.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
prototypes.
What does the new operator do?
lets you create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. constructor functions should never return anything.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof method do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. check left of instanceof to see if it is part of the prototypal chain on the right.
What is a “callback” function?
a function passed to another function as argument.
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()
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
immediate
What do setTimeout() and setInterval() return?
a numeric, non-zero value which identifies the timer created by the call
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
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
they both share a prototype. prototype of element is node