JavaScript Flashcards
What is the purpose of variables?
to store information
How do you declare a variable?
by using a variable keyword following by a variable name
How do you initialize (assign a value to) a variable?
using the assignment operator
What characters are allowed in variable names?
all letters
What does it mean to say that variable names are “case sensitive”?
it has to be spelled exactly the same
What is the purpose of a string?
it can store letters and other characters
What is the purpose of a number?
- stores number
- for tasks that include counting or calculating, also controlling loops
What is the purpose of a boolean?
stores a value of either true or false
helpful to determine which part of the script should run
What does the = operator mean in JavaScript?
assignment operator say that you are going to 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?
null is an assigned value and undefined means the var has been declared but not defined
Why is it a good habit to include “labels” when you log values to the browser console?
you can start debugging or clarity
Give five examples of JavaScript primitives.
string , number , boolean , undefined and null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
joins the string variables being used
What purpose(s) does the + plus operator serve in JavaScript?
for addition and concatenation
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
it places the original variable in front what is being added
What are objects used for?
used to store data that correspond with one another
What are object properties?
key value pair
Describe object literal notation.
the object is the curly braces and their contents (property, values, methods)
How do you remove a property from an object?
use 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?
storing a list of values
Describe array literal notation.
two square brackets with commas in between each value
How are arrays different from “plain” objects?
the key for each value is its index number
What number represents the first index of an array?
0
What is the length property of an array?
returns the amount of objects in an array
How do you calculate the last index of an array?
the amount you have - 1
What is a function in JavaScript?
it is collection of code that is reusable and easier to read
Describe the parts of a function definition.
function keyword, optional name , parameters, open curly brace , optional return statement, closing curly brace
Describe the parts of a function call.
functions name, arguements surround by ()
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call is what is used to call the code itsels with arguements and the definition is what is used to define it
What is the difference between a parameter and an argument?
parametes are what it will take and argument is what it will give
Why are function parameters useful?
be able to use what you pass as a parameter
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?
for debugging and an easy way to inspect your variables in the browser
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?
method is a function that belongs to 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?
floor() method
How do you generate a random number?
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?
using console.log() or MDN
Roughly how many string methods are there according to the MDN Web docs?
20 or more
Is the return value of a function or method useful in every situation?
no, depends on what you are doing
Roughly how many array methods are there according to the MDN Web docs?
40 or more
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?
executes a statement if a specified condition is truthy.
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
-if (condition)
statement1
[else
statement2]
What are the three logical operators?
And && or || Nullish coalescing operator (??)
How do you compare two different expressions in the same condition?
(a > 0 && b > 0)
What is the purpose of a loop?
to repeatedly run a block of code - until a certain condition is met. they repeat an action some number of times
What is the purpose of a condition expression in a loop?
to let it know when to give it an okay , …expression evaluates to true, statement is executed
What does “iteration” mean in the context of loops?
keeps going through each line of code
When does the condition expression of a while loop get evaluated?
before iteration
When does the initialization expression of a for loop get evaluated?
once before the loop begins
When does the condition expression of a for loop get evaluated?
evaluated before each loop iteration
When does the final expression of a for loop get evaluated?
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?
break
What does the ++ increment operator do?
increments the value by 1 , postfix(x++) , prefix(++x)
How do you iterate through the keys of an object?
for(var key in Object)
What event is fired when a user places their cursor in a form control?
focus event
What event is fired when a user’s cursor leaves a form control?
blur event
What event is fired as a user changes the value of a form control?
input event
What event is fired when a user clicks the “submit” button within a ?
submit event
What does the event.preventDefault() method do?
tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
What does submitting a form without event.preventDefault() do?
refreshes the page
What property of a form element object contains all of the form’s controls.
elements | returns an HTMLFormControlsCollection listing all the form controls contained in the element.
What property of form a 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?
bad code in the beginning
What is an advantage of having your console open when writing a JavaScript program?
debugging and knowing whats inside your variables in real time
What is the event.target?
The target property of the Event interface is a reference to the object onto which the event was dispatched.
What is the affect of setting an element to display: none?
Turns off the display of an element so that it has no effect on layout
What does the element.matches() method take as an argument and what does it return?
method checks to see if the Element would be selected by the provided selectorString…checks if the element “is” the selector
Returns a boolean
How can you retrieve the value of an element’s attribute?
The getAttribute() method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or “” (the empty string)
At what steps of the solution would it be helpful to log things to the console?
all steps
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?
listen to each individual tab and have multiple listeners;
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?
several conditionals and have specific classes
What is JSON?
JSON is a text-based data format following JavaScript object syntax
What are serialization and deserialization?
- Serialization is the process of turning an object in memory into a stream of bytes
- Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Why are serialization and deserialization useful?
you save it for another time
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify(value[, replacer[, space]])
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse(text[, reviver])
How to you store data in localStorage?
localStorage.setItem(keyName, keyValue); return is undefined
How to you retrieve data from localStorage?
localStorage.setItem(keyName, keyValue);
What data type can localStorage save in the browser?
string or DOMstring
When does the ‘beforeunload’ event fire on the window object?
event is fired when the window, the document and its resources are about to be unloaded.
What is a method?
A method is a function which is a property of an object.
How can you tell the difference between a method definition and a method call
- property: function(args){ }
- object.method(parameters)
Describe method definition syntax (structure).
property: function(args){ }
Describe method call syntax (structure).
object.method(parameters)
How is a method different from any other function?
method is only associated with the object it is created from
What is the defining characteristic of Object-Oriented Programming?
pairs data with behaviour
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation , Inhereitance , Polymorphism
What is “abstraction”?
is a way to reduce complexity and allow efficient design and implementation in complex software systems
What does API stand for?
Application Programming Interface
What is the purpose of an API?
an API (application programming interface) simplifies programming by abstracting the underlying implementation and only exposing objects or actions the developer needs.
What is this in JavaScript?
always a reference to an object
What does it mean to say that this is an “implicit parameter”?
meaning that 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?
character
Given the above character object, what is the result of the following code snippet? Why?
its a me Mario! because this is getting the objects first name
Given the above character object, what is the result of the following code snippet? Why?
It’s-a-me, undefined! because this is trying to access the var hello’s firstname property
How can you tell what the value of this will be for a particular function or method definition?
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 is for a particular function or method call?
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.
What kind of inheritance does the JavaScript programming language use?
prototyoe-based
What is a prototype in JavaScript?
object that contains methods,strings arrays and numbers that can be used by others
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?
prototypes
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
proto object
What does the new operator do?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value
What is a “callback” function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
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,delay) method
How can you set up a function to be called repeatedly without using a loop?
setInterval(function, delay) method
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
milliseconds (thousandths of a second)
What do setTimeout() and setInterval() return?
The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval();
What is AJAX?
is a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest.
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 (XHR)
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?
This interface also inherits properties of XMLHttpRequestEventTarget and of EventTarget. // Property