JavaScript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
Var variablename= variable value
How do you initialize (assign a value to) a variable?
=
What characters are allowed in variable names?
Letter, $, or _, numbers after first character
What does it mean to say that variable names are “case sensitive”?
Variable name can be different for capital or lowercase same name
What is the purpose of a string?
Store text a sequence of characters
What is the purpose of a number?
Store a number, calculations
What is the purpose of a boolean?
Store true or false values, render a sate of something is or isn’t
What does the = operator mean in JavaScript?
Assignment operator, assigning a value to the variable.
How do you update the value of a variable?
Variablename = newvalue
What is the difference between null and undefined?
Null: value can only be assigned, not generated, developer set variable = null purposely. placeholder empty for now
Undefined: empty, nothing here
Why is it a good habit to include “labels” when you log values to the browser console?
To understand which console log is being output
Give five examples of JavaScript primitives.
Number, String, Boolean, Undefined, Null and Symbol
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Joining together 2 or more strings, result will be a string
What purpose(s) does the + plus operator serve in JavaScript?
cocatenate or add
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
The addition assignment (+=) operator adds the value of the right operand to a variable and assigns the result to the variable
What are objects used for?
Grouping together related variables
What are object properties?
Piece of data stored in the object, collection of variables
The key and value in the objects
Describe object literal notation.
Var object = {
Key: value,
Key: value
};
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 object.property
Bracket notation object[property]
What are arrays used for?
Lists of data where either order is important or not important
Describe array literal notation.
Var arrayname;
Arrayname = [‘value1’, ‘value2’];
bracket separated by commas
How are arrays different from “plain” objects?
has a length property and keeps updating
call a method to add data to the array
What number represents the first index of an array?
0
What is the length property of an array?
Number of items in the array
How do you calculate the last index of an array?
Array.length -1
What is a function in JavaScript?
Performs a task and returns the output
Describe the parts of a function definition.
Function(called keyword) functname (parameters) { Code block;
Return ;
}
Describe the parts of a function call.
functionname(argument1, argument2, etc);
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function call is running the code
Function definition is creating the object, has a codeblock { } and function keyword
What is the difference between a parameter and an argument?
Parameter is a placeholder for a variable we don’t know until we call the function and pass an argument
When defining a function, we declare parameters.
When we call a function we pass it an argument
Why are function parameters useful?
So we can write the function once and insert any argument we want when we run the function
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 (get the output to wherever the function was called)
Prevents any more code in the function’s code block from being run (stops function from running)
Give 6 examples of comparison operators.
> , <, >=, <=, ===, and !==
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
To guide the computer to make a decision based on criteria
Is else required in order to use an if statement?
No If you require code to run only when the statement returns true
Describe the syntax (structure) of an if statement.
If (condition) {
code block -Return value;
} else { code;
}
What are the three logical operators?
&& || !
How do you compare two different expressions in the same condition?
Using a && or ||
Why do we log things to the console?
To find errors and warnings as they occur in the JavaScript code.
Debugging, something is broken, understand what’s happening
What is a method?
a function which is a property of an object. Using an object then a . then method name, performs a function
How is a method different from any other function?
method is associated with an object, while a function is not.
method: object.method
function: state it
How do you remove the last element from an array?
arrayname.pop();, arrayname.shift() removes first element
How do you round a number down to the nearest integer?
Math.floor(numberhere);
How do you generate a random number?
Math.random() -random float value from 0 to 1 (is a percentage)
How do you delete an element from an array?
arrayname.splice();
splice method at the index array
How do you append an element to an array?
arrayname.push() adds to end/ arrayname.unshift() adds to beginning
How do you break a string up into an array?
stringname.split(“ “) use space as an argument (thing you want to break it up on)
Do string methods change the original string? How would you check if you weren’t sure?
does not change it, console log it
Is the return value of a function or method useful in every situation?
No
What is the purpose of a loop?
To check a condition and repeat the code block until a condition is met
What is the purpose of a condition expression in a loop?
Instruct the code when to stop looping
What does “iteration” mean in the context of loops?
A pass through the loop
When does the condition expression of a while loop get evaluated?
before each pass through the loop (before each iteration)
When does the initialization expression of a for loop get evaluated?
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?
When the condition evaluates to false
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
How do you iterate through the keys of an object?
use a for in loop
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
What does the event.preventDefault() method do?
prevent the browser from automatically reloading the page with the form’s values in the URL
What does submitting a form without event.preventDefault() do?
Prevent form from submitting
What property of a form element object contains all of the form’s controls.
HTMLFormElement.elements
What property of a form control object gets and sets its value?
HTMLFormElement.elements.value
What is one risk of writing a lot of code without checking to see if it works so far?
Not knowing where to fix it , rewriting
What is an advantage of having your console open when writing a JavaScript program?
See errors as they occur
What is the event.target?
Target of the event, element interacted width
What is the effect of setting an element to display: none?
Removes it from the display and doesn’t take up space but still part of the page
What does the element.matches() method take as an argument and what does it return?
A string css selector and returns a boolean if it matches the selector
How can you retrieve the value of an element’s attribute?
Element.getAttribute(‘ attribute you want here‘);
At what steps of the solution would it be helpful to log things to the console?
Every step, after every expression
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?
conditional for each possible view or separate event handler for each view
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?
individual conditional blocks for each view, repetition of effort same code
What is JSON?
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 so you can do stuff like store it on disk or send it over the network.
Deserialization is the reverse process: turning a stream of bytes into an object in memory.
Serialization converts it to a string
Deserialization does the opposite to objects and arrays
Why are serialization and deserialization useful?
Serialization lets you send the data easily
Deserialization lets you convert back to an object that is easy to work with
How do you serialize a data structure into a JSON string using JavaScript?
var jsonString = JSON.stringify(data structure here);
How do you deserialize a JSON string into a data structure using JavaScript?
var jsonParse = JSON.parse(jsonString);
How do you store data in localStorage?
localStorage.setItem(‘key name’, ‘value name’);
How do you retrieve data from localStorage?
Var varstorage = localStorage.getItem(‘keyName’);
What data type can localStorage save in the browser?
String only
When does the ‘beforeunload’ event fire on the window object?
fired when the window, the document and its resources are about to be unloaded. before page closes
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?
A method definition has the code block
Method call is running the code block with a argument
Describe method definition syntax (structure).
var obj = {
propertyName : function def(parameter) {
}
};
Describe method call syntax (structure).
objectname.method(arguments);
How is a method different from any other function?
Methods are associated with an object, exampleobject.methodname
Functions run on their own
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”?
being able to work with (possibly) complex systems in simple ways. lightswitches
What does API stand for?
application programming interface
What is the purpose of an API?
Application that is gonna interface with another program to do things in a simple way
What is this in JavaScript?
an implicit parameter of all JavaScript functions.
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);
}
};
nothing, doesn’t exist yet bc its not called
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
The variable message : “it’s a me mario’. Runs the greet property of the character variable which is a function.
greet method being invoked of he character object. the object being called off of is chjaracer and gets firstname Property
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
no dot in hello() so becomes the window, has nofirstname, returns “it’s a me undefined”
How can you tell what the value of this will be for a particular function or method definition?
You can’t, undefined. only when being called
How can you tell what the value of this is for a particular function or method call?
the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).
“This” is “the object to the left of the dot”. if no dot, it is window
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance
What is a prototype in JavaScript?
object that can allow JavaScript objects to inherit features from one another
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
JavaScript objects give certain behaviors (methods) or data (properties) to other objects.
within prototype object
each of those methods a re stored on the prototype object of that kind of data type
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
Prototype
What does the new operator do?
1 Creates a new blank object, attached to the prototype of the new object
2 Takes value of the prototype constructor
3 Makes the new object the value of “this” and runs code block
4 If No return statement, the object created gets returned automatically
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
.prototype
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. The return value is a boolean value.
What is a “callback” function?
passing a function as an argument to another function
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()?
0 seconds, immediate
What do setTimeout() and setInterval() return?
Return a intervalID a numeric, non-zero value which identifies the timer created by the call to
setInterval(); this value can be passed to clearInterval() to cancel the interval.
starts at 1, pos integer
What is AJAX?
Technique for loading data into part of a page without having to reload 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
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototype, inherits
What is Array.prototype.filter useful for?
array.filter copies, doesnt modify and only values you want
What is Array.prototype.map useful for?
modifying all values in a copy
What is Array.prototype.reduce useful for?
combining all the elements of a big array to one