Javascript Flashcards
What is the purpose of variables?
To store data
How do you declare a variable?
Using the variable keyword and variable name
How do you initialize (assign a value to) a variable?
Write the variable name, and use the assignment operator to assign it to a value
What characters are allowed in variable names?
The name can contain letters, numbers, dollar sign ($), or an underscore (_).
What does it mean to say that variable names are “case sensitive”?
the capitalization must be consistent
What is the purpose of a string?
The String object is used to represent and manipulate a sequence of characters.
What is the purpose of a number?
represent and manipulate numbers
What is the purpose of a boolean?
to hold data types of true or false
What does the = operator mean in JavaScript?
assign a value into a variable
How do you update the value of a variable?
name of variable, assignment operator and the new value
What is the difference between null and undefined?
undefined has not been assigned a value yet. null intentionally has no value.
Why is it a good habit to include “labels” when you log values to the browser console?
helpful to debug
Give five examples of JavaScript primitives.
string, number, boolean, undefined and null
What data type is returned by an arithmetic operation?
number value
What is string concatenation?
to add two strings so they become a single value
What purpose(s) does the + plus operator serve in JavaScript?
to add numeric values or concatenate one string into another
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
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?
group together a set of variables and functions
What are object properties?
variable that is attached to the object and we can store data
Describe object literal notation.
variable, assigning operator , curly braces, and collection of property and values
How do you remove a property from an object?
use the delete keyword followed by the object name and property
What are the two ways to get or update the value of a property?
using dot notation or bracket notation
What are arrays used for?
It stores a list of values
Describe array literal notation.
variable keyword, variable name, assignment operator, square brackets with the values inside separated by a comma
How are arrays different from “plain” objects?
object: Property name and value, array: numeric index and value
What number represents the first index of an array?
0
What is the length property of an array?
The number of items in the array
How do you calculate the last index of an array?
arrayName.length - 1
What is a function in JavaScript?
a procedure—a set of statements that performs a task or calculates a value
Describe the parts of a function definition.
the function keyword
an optional name
zero or more parameters
{a code block
an optional return statement}
Describe the parts of a function call
The function’s name.
() parentheses, with the arguments inside
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition has the function keyword and a block of code. whereas function call has only the function name followed by the parentheses
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?
They allow a function to work with different data depending on what values are passed to the function when it is called.
What two effects does a return statement have on the behavior of a function?
exits the function’s code block and returns control to the calling function
Why do we log things to the console?
To see what our data is
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?
The difference is that a method is associated with an object, while a function is not.
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?
Math.floor() method
How do you generate a random number?
Math.random() method
How do you delete an element from an array?
Using splice, pop shift slice method
How do you append an element to an array?
Using push or unshift method
How do you break a string up into an array?
Using split(“ “) method
Do string methods change the original string? How would you check if you weren’t sure?
No, they do not. Using console.log
Roughly how many string methods are there according to the MDN Web docs?
50
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?
40
Give 6 examples of comparison operators.
===, !==, >=, <=, >, <
What data type do comparison expressions evaluate to?
Boolean value
What is the purpose of an if statement?
guides a program to make decisions based on specified criteria.
Is else required in order to use an if statement?
No
Describe the syntax (structure) of an if statement.
if keyword, condition expression (), curly braces, and code block
What are the three logical operators?
&& and, ||or and !(bang operator)
How do you compare two different expressions in the same condition?
&& or ||
What is the purpose of a loop?
Check a condition, perform a task a until it breaks a condition
What is the purpose of a condition expression in a loop?
stop running if does not satisfy the codition
What does “iteration” mean in the context of loops?
each time the loop runs
When does the condition expression of a while loop get evaluated?
before executing the expression
When does the initialization expression of a for loop get evaluated?
before any loop action happens
When does the condition expression of a for loop get evaluated?
before each iteration
When does the final expression of a for loop get evaluated?
after each iteration.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break statement. It exists a running loop but it does not exit a function like the ‘return’ statement
What does the ++ increment operator do?
Increment by one
How do you iterate through the keys of an object?
Using for in method
What are the four components of “the Cascade”.
Source Order,Inheritance, Specificity & !important,
What does the term “source order” mean with respect to CSS?
the order that your CSS rules are written in your stylesheet.
How is it possible for the styles of an element to be applied to its children as well without an additional CSS rule?
inheritance. most common: fonts and color
List the three selector types in order of increasing specificity.
element, class and ID
Why is using !important considered bad practice?
it breaks the natural CSS cascading in stylesheets and is difficult to debug
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’
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
What does submitting a form without event.preventDefault() do?
it causes the form to refresh the page and to submit the data
What property of a form element object contains all of the form’s controls.
elements property
What property of a form control object gets and sets its value?
the value property
What is the affect of setting an element to display: none?
Hides the element and completely removes the element from the document. So it will no longer have any relationship to any other elements that are on the screen.
What does the element.matches() method take as an argument and what does it return?
Takes a CSS selector. It returns true or false
How can you retrieve the value of an element’s attribute?
By using the getAttribute method and then passing in whatever attribute that we’re trying to access the value of.
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?
If we’re not using event delegation, that would require that we add an eventListener to each new tab we created. One of the huge benefits of event delegation is to list on the parent and no matter how many children we have, the parent can respond to all of those events.
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/else statement that will check each element
How do you store data in localStorage?
Use setItem method
How do you retrieve data from localStorage?
getItem method
What data type can localStorage save in the browser?
JSON String
When does the ‘beforeunload’ event fire on the window object?
Right before the page is loaded
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?
Method call indicates when a method of an object is being called.
Method definition is when you define a function property in an object.
Describe method definition syntax (structure).
an object, a property, colon, function and block code
Describe method call syntax (structure).
object name.method(arg)
How is a method different from any other function?
a method is associated with an object, while a function is not
What is the defining characteristic of Object-Oriented Programming?
Object can contain not only data and value but functionality in the form of methods.
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
Taking something that is complex from the user perceptive and simplifying it.
What does API stand for?
Application programming interface.
What is the purpose of an API?
to create an accessible way to extract and share data within and across organizations.
What is this in JavaScript?
This refers to the object that was generated by a constructor and its going to refer to itself.
Refers to the calling object. Whatever object that is calling the function. ‘This’ is going to be defined based on who is calling the function.
What does it mean to say that this is an “implicit parameter”?
it is available in the function’s code block even though it was never declared or included in the function’s parameter list
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);
}
};
The object ‘character’.
Given the above character object, what is the result of the following code snippet? Why? character.greet();
Its’ -a-me Mario. Because The character is the calling object.
And the character object has firstName property.
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! Because the rest of the properties of the ‘character’ object, like firstName, was not saved into the hello variable. ‘Hello’ its not attached into an object, it gets attached to the window object. And the window object does not have a firstName property on it.
How can you tell what the value of this will be for a particular function or method definition?
We could assume but we can not know.
How can you tell what the value of this is for a particular function or method call?
If we can see the object that is calling it,’this’ will refer to that object
What kind of inheritance does the JavaScript programming language use?
prototype-based inheritance
What is a prototype in JavaScript?
An object that contains the functionality that you want to prototype into another object. Basically, a framework of functionality that you can use to attach to any object that you create.
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?
Because of prototyping we can call those methods.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
“prototype” chain
What does the new operator do?
The new keyword is for creating new object instances.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype
What does the instanceof operator do?
tests to see if the prototype of a constructor appears in the prototype of an object.
What is a “callback” function?
callback function is a function passed into another function as an 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 function
How can you set up a function to be called repeatedly without using a loop?
Using setInterval() function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
a value of 0, meaning its ready to be called. does not mean that it will be executed immediately
What do setTimeout() and setInterval() return?
positive integer value
What is AJAX?
Asynchronous JavaScript And XML is a programming practice that allows you to collect data to update parts of the DOM of an HTML page without the need for a full page refresh
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
new XMLHttpRequest()
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
xhr.addEventListener(‘load’)
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
They share a prototype