Javascript Flashcards
What is the purpose of variables?
to store date, to call upon the value later.
How do you declare a variable?
you would write the var then variable name , or identifier. If it has more than one name, it is written in camelCase
[var name:]
How do you initialize (assign a value to) a variable?
you would write the identifier then the variable value;
[name = 420;]
What characters are allowed in variable names?
letters,numbers, dollar sign($) or and underscore.
What does it mean to say that variable names are “case sensitive”?
It needs to be written exactly how it is typed. meaning
thisVariable is not the same as thisvariable
What is the purpose of a string?
to give values that contain letters or other characters.
What is the purpose of a number?
to give a numerical value to a variable. Normally for it use as counting/mathematics.
What is the purpose of a boolean?
to define the var as a true/false value.
What does the = operator mean in JavaScript?
it is the assignment operator. it gives us the ability to assign a value to the variable.
How do you update the value of a variable?
you reassign the variable. you wont need to use the var to reassign the variable.
What is the difference between null and undefined?
null is an intentionally nonexistent or invalid object, while undefined has no given value at all, or unintentional.
Why is it a good habit to include “labels” when you log values to the browser console?
It helps identify what the value is related to, if there is no tag. It will just give out the string/value with no context.
Give five examples of JavaScript primitives
String, number, boolean, undefined, and null.
What data type is returned by an arithmetic operation?
a number value
What is string concatenation?
to join to variables into one.
var fullName = firstName + lastName;
What purpose(s) does the + plus operator serve in JavaScript?
It is used to add one value to another
What data type is returned by comparing two values (, ===, etc)?
boolean; true or false
What does the += “plus-equals” operator do?
It lets us add two values together, the result will then be assigned.
What are objects used for?
Objects are for RELATED data.
What are object properties?
A key value pair, a piece of data that is attached under an object with a unique name. The value may change, but the name will stay the same.
Describe object literal notation
You need { }, to store value, you need property name:(value)
How do you remove a property from an object?
use the delete
What are the two ways to get or update the value of a property?
use dot notation or bracket notation.
dot
object.property = (new value);
bracket
object[‘property’] = (new value);
Describe array literal notation.
a variable will x amount of values separated by commas (usually in the same line)
you can store strings, numbers, and booleans
How are arrays different from “plain” objects?
arrays has data is relatable to one another, for example colors. Objects have relatable data to a certain object. (name, height, weight, occupation etc)
What number represents the first index of an array?
the first array value is INDEX 0 (zero)
It starts at zero because it the first item is zero index’s away from the BEGINNING of the array.
What is the length property of an array?
it will show how many Items are within each array.
for example:
var colors = [“white”, “black, “brown”, “green”]
has 4 items in. So:
numColors = colors.length;
should equal numColors = 4;
How do you calculate the last index of an array?
(# of items in array) - 1
Describe the parts of a function definition.
the [function] keyword an optional name a comma-separated list of zero or more parameters. start of the functions' code block' the end of the function's code block
function example(parameter 1, parameter 2....) { //....more javascript code... return; }
Describe the parts of a function call.
function name comma separated list of zero or more arguments
sayHello ();
When comparing them side-by-side, what are the differences between a function call and a function definition?
describing the function is creating the function and what it will do. Calling the function will practically “using” the function only.
What is the difference between a parameter and an argument?
parameter is a placeholder, it is variable that is unknown until we call the function.
A argument is the value that replaces the parameter value in the function.
Why are function parameters useful?
the parameters will let us change the value of the arguments when we call the function
What two effects does a return statement have on the behavior of a function?
Causes the function to produce a new value.
prevents any more code in the function’s code block from being run.
Why do we log things to the console?
to test out our code and to see if it runs properly.
What is a method?
a function which is a property of an object.
console.log is a method. it belongs to the object, console.
How is a method different from any other function?
a method is attached to a object.
How do you remove the last element from an array?
using the .pop() method
How do you round a number down to the nearest integer?
you use the Math.floor()
How do you generate a random number?
you use Math.random();
How do you append an element to an array?
.push() method.
Is the return value of a function or method useful in every situation?
No, the return is not always useful in every situation.
Give 6 examples of comparison operators.
==
<
>
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
to execute a statement if a certain statement is true
Is else required in order to use an if statement?
no. it is not required
Describe the syntax (structure) of an if statement.
It begins with if (conditions.) [
coding block}
What are the three logical operators?
&& logical and
|| logical or
! logical not
How do you compare two different expressions in the same condition?
Logical and/or operators
!! &&
when does the condition expression of a for loop get evaluated?
Before each loop iteration
When does the final expression of a for loop get evaluated?
at the end of each loop iteration
Besides a return statement, which exits its entire function block, which keywrod exits a loop before it condition expression evaluates to false?
break
What does the ++ increment operator do?
adds one to the variable
What event is fired when a user places their cursor in a form control?
‘focus’ event is fired
What event is fired when a user’s cursor leaves a form control?
‘blur’ event is fired
What event is fired as a user changes the value of a form control?
‘input’ event is fired
What event is fired when a user clicks the “submit” button within a ?
‘submit’ event is fired
What does the event.preventDefault() method do?
this prevents the page from refreshing.
What does submitting a form without event.preventDefault() do?
it will refresh the page. because it will reset the form.
What property of a form element object contains all of the form’s controls.
elements
What property of a form control object gets and sets its value?
the value property
example:
document.query(‘form’).name.value
this will get the value that is inputted in the ‘Name’ input.
What is one risk of writing a lot of code without checking to see if it works so far?
It will consist of a lot of bugs. so along the way it makes it easier to pinpoint what is exactly wrong.
What is an advantage of having your console open when writing a JavaScript program?
To check if all of your javascript codes/functions work properly and the output of those codes/functions are correct.
What is the event.target?
the event target is the location where teh Event has started/fired from
What is the affect of setting an element to display: none?
it will hide the element like it doesnt exist
What does the element.matches() method take as an argument and what does it return?
it takes a selector string as the argument.
element.matches(‘.selector’)
How can you retrieve the value of an element’s attribute?
using the getAttribute Method
element.getAttribute( )
At what steps of the solution would it be helpful to log things to the console?
during conditional statements. to see if the function runs pass it.
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 listen to eventListener 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?
I would probably use hyperlinks. that would link certain elements to different pages. instead of manipulating the information.
What is a method?
a function which is a property of an object. There are two kinds of methods;
instance methods which are build-in task performed instance or static mthods which are tasks that are called directyly on an objet constructor.
How can you tell the difference between a method definition and a method call?
method definition includes the function () and what the method will do when it is called.
var doggo = { bork: function() { return 'bork! bork! bork!': } }
method call is when the method is only attached to the object. such as
Object1.reverse( );
Describe method definition syntax (structure).
var doggo = { bork(method name): function() { code block; }
Describe method call syntax (structure).
object.methodCall ();
How is a method different from any other function?
methods are attached to objects and can be only used on the object that it is attached to.
What is the defining characteristic of Object-Oriented Programming?
It is based on containing related data and the functions to objects. and the object is the only thing that can alter the object.
data and behavior is intertwined
What are the four “principles” of Object-Oriented Programming?
1) Abstraction : removing physical, spatial, or temporal details.
2) Encapsulation : it is the bundling of data with the methods that operate on that data or restricting of direct access to some of an objects components.
3) Inheritance: is the mechanism of basing an object or class upon another object or
4) Polymorphism
What is “abstraction”?
being able to work with complex things in simple ways.
What does API stand for?
Application programming interface .
What is the purpose of an API?
It is to give programmers a way to interact with a system in a simplified, consistent fashion. “abstraction”.
What is this in JavaScript?
is an implicit parameter of all Javascript functions.
[this] is going to reference the object that it is currently in. if there is no reference to an object, [this] is referencing the window.
What does it mean to say that this is an “implicit parameter”?
that it is available in a functions code block even though it was never included in the functions parameter list or declared with var
When is the value of this determined in a function; call time or definition time?
it is determined at 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); } };
2)Given the above character object, what is the result of the following code snippet? Why?
Given the above character object, what is the result of the following code snippet? Why?
character.greet is being called and it will return the firstName that it was assigned by using this.firstName.
this = the var character.
so this.firstName should return the string ‘Mario’
2) The result should be ‘It’s-a-me, Mario!’ because this.firstName is referencing to the objects (that this is placed in) firstName.
How can you tell what the value of this will be for a particular function or method definition?
you can tell what [this] will be because it is attached to an object. so [this] is referring the object that it is contained in.
How can you tell what the value of this is for a particular function or method call?
if you cannot see the function that is being called. you dont know what the value of [this] will be
What kind of inheritance does the JavaScript programming language use?
prototype-based (or prototypal) inheritance.
What is a prototype in JavaScript?
JavaScript prototype is simply 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?
you use Object.setPrototypeOf(Obj, prototype)
Object.setPrototypeOf(doggo, petPrototype);
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
it looks for the prototype that the object has inherited.
Object.setPrototypeOf(doggo, petPrototype)
so above, if doggo does not have a given method, it will look at petPrototype for that given method.
What is a “callback” function?
it is a function that is passed into another function as an argument, which is then invoked inside the outer function to complete some kin 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?
setTimerout( ) method.
it sets a timer which executes a function or specified piecoe of code once the timer expires.
It is a global method, so it can be called with no object to the left of it .
How can you set up a function to be called repeatedly without using a loop?
setInterval(function, code, [delay], [arguments after it ends] ) method
it repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. it returns an interval ID with uniquely identifies the interval so you can remove it late by calling clearInterval( )
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
100 milliseconds
What do setTimeout() and setInterval() return?
is a numeric, non zero value which identifies the timer created by the call to setInterval
What is AJAX?
It is a technique for loading data into PART of a page without having to refresh the page. The data is often sent in a format called JavaScript Object Notation (or JSON)
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
XMLHttpRequest will work asynchronously unless stated otherwise.
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
the ‘load’ event is fired.
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
they are able to share teh same functionality because of prototype methods.
What is Array.prototype.map useful for?
to manipulate the the array and create a new array with a function predicate. for example
multiplying all numbers of an array by 2
numbersArray.map(number => number * 2);
What is Array.prototype.reduce useful for?
to combine values/objects in an array