JavaScript Flashcards
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What is the purpose of variables?
Variables are used to store data for the computers to use in the future
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
How do you declare a variable?
use a keyword (var, let, const) and variable name and =
Example: var example = 100;
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
How do you initialize (assign a value to) a variable?
use an equal sign
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What characters are allowed in variable names?
letter, numbers, $, underscore,
numbers cant be first
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What does it mean to say that variable names are “case sensitive”?
Car= and car= are different variables
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What is the purpose of a string?
for storing text that wouldn’t make sense to JavaScript
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What is the purpose of a number?
For doing calculations;
If the number is a zipcode then store as string;
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What is the purpose of a boolean?
it is for letting computers make a decision that is true or false
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What does the = operator mean in JavaScript?
assignment operator
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
How do you update the value of a variable?
Update the value of a variable by assigning a new value to the variable name without the keyword
Example: let a = 2; for declaring a variable a = 5; for updating a variable (no need for keyword)
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
What is the difference between null and undefined?
- null is absents of value intentionally
(ex: optional user input) - undefined is not trustworthy
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
Why is it a good habit to include “labels” when you log values to the browser console?
Label within the console.log to know where you’re getting values from / for organization purposes
JAVASCRIPT-PRIMITIVES-AND-VARIABLES
Give five examples of JavaScript primitives.
string, number, boolean, null, and undefined
JAVASCRIPT-OPERATORS-AND-EXPRESSIONS
What data type is returned by an arithmetic operation?
numeric data
JAVASCRIPT-OPERATORS-AND-EXPRESSIONS
What is string concatenation?
Process of joining together two or more strings to create one new string
JAVASCRIPT-OPERATORS-AND-EXPRESSIONS What purpose(s) does the + plus operator serve in JavaScript?
adds one value to another;
addition of numbers and concatenation of strings
JAVASCRIPT-OPERATORS-AND-EXPRESSIONS
What data type is returned by comparing two values
(, ===, etc)?
booleans (true or false)
JAVASCRIPT-OPERATORS-AND-EXPRESSIONS
What does the += “plus-equals” operator do?
left operand = left operand + right operand
Example: 2 += 3 = 5
JAVASCRIPT-OBJECTS
What are objects used for?
objects {} are used group together a set of variables and functions to create a model / representation of something
JAVASCRIPT-OBJECTS
What are object properties?
properties tell us about the object;
the variable of an object;
Example: name, age, etc.
JAVASCRIPT-OBJECTS
Describe object literal notation.
Store object in variable, within opening and closing curly braces are properties and their values;
Example: var object = { properties: value }
JAVASCRIPT-OBJECTS
How do you remove a property from an object?
Use delete
Example:
delete object.property or delete object[‘property’]
JAVASCRIPT-OBJECTS
What are the two ways to get or update the value of a property?
dot notation or bracket notation
JAVASCRIPT-ARRAYS
What are arrays used for?
Array are used for storing values in a grouped list like a grocery list where orders are not essential;
JAVASCRIPT-ARRAYS
Describe array literal notation.
var array = [ ]
JAVASCRIPT-ARRAYS
How are arrays different from “plain” objects?
Objects have individual assigned properties but arrays have automatic number indexes assigned
JAVASCRIPT-ARRAYS
What number represents the first index of an array?
0 / Zero
JAVASCRIPT-ARRAYS
What is the length property of an array?
The length property is used to find the length of an array
JAVASCRIPT-ARRAYS
How do you calculate the last index of an array?
array.length - 1
JAVASCRIPT-FUNCTION
What is a function in JavaScript?
a reusable block of code
JAVASCRIPT-FUNCTION
Describe the parts of a function definition.
keyword, optional name, optional number of parameters, code, return
JAVASCRIPT-FUNCTION
Describe the parts of a function call.
name, ( ), and arguments
JAVASCRIPT-FUNCTION
When comparing them side-by-side, what are the differences between a function call and a function definition?
function call doesn’t have keyword function and uses ( ) to call the function
JAVASCRIPT-FUNCTION
What is the difference between a parameter and an argument?
parameter = placeholder for potential value; argument = actual value being placed in the argument;
JAVASCRIPT-FUNCTION
Why are function parameters useful?
Function parameters are useful because they pass information to a function in order to be able to reuse the function code block
JAVASCRIPT-FUNCTION
What two effects does a return statement have on the behavior of a function?
1. Will replace the function called in that line of code; Example: function(3) = 10 var x = function(3) ... will become var x = 10
- Stops the function entirely once a value is returned
JAVASCRIPT-METHODS
Why do we log things to the console?
to debug our code, to check if our output is as expected, etc.
JAVASCRIPT-METHODS
What is a method?
function that is a property of an object
JAVASCRIPT-METHODS
How is a method different from any other function?
methods have to be called on an object;
functions do not;
JAVASCRIPT-METHODS
How do you remove the last element from an array?
pop( )
pop method
JAVASCRIPT-METHODS
How do you round a number down to the nearest integer?
Math.floor( )
JAVASCRIPT-METHODS
How do you generate a random number?
Math.random( )
JAVASCRIPT-METHODS
How do you delete an element from an array?
pop for last item;
shift for first item;
splice for specific index
JAVASCRIPT-METHODS
How do you append (to the end) an element to an array?
push( )
Push method
JAVASCRIPT-METHODS
How do you break a string up into an array?
split(‘ ‘)
Split method
JAVASCRIPT-METHODS
Do string methods change the original string? How would you check if you weren’t sure?
does not change the original string;
To check: console.log( )
JAVASCRIPT-METHODS
Roughly how many string methods are there according to the MDN Web docs?
About 50
JAVASCRIPT-METHODS
Is the return value of a function or method useful in every situation?
No, not in every situation
JAVASCRIPT-METHODS
Roughly how many array methods are there according to the MDN Web docs?
A lot
JAVASCRIPT-METHODS
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
which stands for: Mozilla Developer Network
JAVASCRIPT-IF
Give 6 examples of comparison operators.
== is equal to; != is not equal to; === is strictly equal to; !== is strictly not equal to; > is greater than; < is less than; >= is greater than or equal to; <= is less than or equal to;
JAVASCRIPT-IF
What data type do comparison expressions evaluate to?
Boolean (true or false)
JAVASCRIPT-IF
What is the purpose of an if statement?
An if statement evaluates (or checks) a condition.
Let the computer make a decision based on conditions.
JAVASCRIPT-IF
Is else required in order to use an if statement?
no, only if you want to provide more than one set of code / another execution
JAVASCRIPT-IF
Describe the syntax (structure) of an if statement.
if (condition)…
instruction: { } else { }
JAVASCRIPT-IF
What are the three logical operators?
&& (and) ;
|| (or);
! (not);
JAVASCRIPT-IF
How do you compare two different expressions in the same condition?
&& (and);
|| (or);
JAVASCRIPT-LOOPS
What is the purpose of a loop?
To continuously iterate through a code block until the condition is met
Allows us to repeat a certain function
JAVASCRIPT-LOOPS
What is the purpose of a condition expression in a loop?
To know when to stop the loop
JAVASCRIPT-LOOPS
What does “iteration” mean in the context of loops?
Iteration = looping through once
JAVASCRIPT-LOOPS
When does the condition expression of a while loop get evaluated?
At the start of every loop
JAVASCRIPT-LOOPS
When does the initialization expression of a for loop get evaluated?
Before the loop begins and only once
JAVASCRIPT-LOOPS
When does the condition expression of a for loop get evaluated?
Before each iteration
JAVASCRIPT-LOOPS
When does the final expression of a for loop get evaluated?
After each iteration
JAVASCRIPT-LOOPS
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break
JAVASCRIPT-LOOPS
What does the ++ increment operator do?
increments by 1
JAVASCRIPT-LOOPS
How do you iterate through the keys of an object?
For in loop
DOM-QUERYING
Why do we log things to the console?
To see what we’re doing and for debugging
DOM-QUERYING
What is a “model”?
representation of something
DOM-QUERYING
Which “document” is being referred to in the phrase Document Object Model?
the html document
DOM-QUERYING
What is the word “object” referring to in the phrase Document Object Model?
object is a data type
DOM-QUERYING
What is a DOM Tree?
organization of/chunk of document
representative chunk of the page that shows the element and their element’s configuration
DOM = model that represents an html document in the form of javascript data types
DOM-QUERYING
Give two examples of document methods that retrieve a single element from the DOM.
document. querySelector( );
document. getElementById( );
DOM-QUERYING
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll( )
DOM-QUERYING
Why might you want to assign the return value of a DOM query to a variable?
to be able to use it again in the future
DOM-QUERYING
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
Console.dir( ) is the way to see all the properties of a specified JavaScript object in console by which the developer can easily get the properties of the object.
DOM-QUERYING
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
HTML loads from top to bottom and the elements need to load first
DOM-QUERYING
What does document.querySelector() take as its argument and what does it return?
The document.querySelector( ) takes
a css selector as an argument
and
returns first one that matches
DOM-QUERYING
What does document.querySelectorAll( ) take as its argument and what does it return?
The document.querySelectorAll( ) takes
a css selector as an argument
and
returns a NodeList
DOM-EVENTS
Why do we log things to the console?
In this exercise…
to debug and check if action happened
DOM-EVENTS
What is the purpose of events and event handling?
when user does something, another thing happens
- something that happens
- to set up a list of action whenever an event occurs
DOM-EVENTS
Are all possible parameters required to use a JavaScript method or function?
no they are not;
3rd parameter of addEventListener is optional
DOM-EVENTS
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener( )
DOM-EVENTS
What is a callback function?
a function that is called as an argument in another function
DOM-EVENTS
What object is passed into an event listener callback when the event fires?
an object that is built in the moment as a reference of data
all relevant data is created, therefore you could get specific information in that one specific occurrence
DOM-EVENTS
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
the button that you interact with;
target = the dom element where the event originated from;
If you weren’t sure, then console.log(event.target);
You could get more information about it in MDN
DOM-EVENTS
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick( ) )
2nd one is an anonymous function which may result in undefined.
- Always gets called without having to click it
DOM-MANIPULATION
What is the className property of element objects?
Property that can get or set the value of class attribute
DOM-MANIPULATION How do you update the CSS class attribute of an element using JavaScript?
Use the className property on an element;
select your element.className = new value;
DOM-MANIPULATION
What is the textContent property of element objects?
to get or set a text element
DOM-MANIPULATION
How do you update the text within an element using JavaScript?
select your element.textContent = ‘sample’
DOM-MANIPULATION
Is the event parameter of an event listener callback always useful?
No, if it’s not necessary, then no need.
DOM-MANIPULATION
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
More complicated if we did not have variable
DOM-MANIPULATION
Why is storing information about a program in variables better than only storing it in the DOM?
Makes it easier to know what’s going on in your own code.
Store it in Javascript in variables and use that data to influence the DOM.
JAVASCRIPT-FORMS
What event is fired when a user places their cursor in a form control?
focus event
JAVASCRIPT-FORMS
What event is fired when a user’s cursor leaves a form control?
blur event
JAVASCRIPT-FORMS
What event is fired as a user changes the value of a form control?
input event
JAVASCRIPT-FORMS
What event is fired when a user clicks the “submit” button within a form?
submit
JAVASCRIPT-FORMS
What does the event.preventDefault() method do?
Stops the default behavior from happening
In this exercise… stop browser from refreshing
JAVASCRIPT-FORMS
What does submitting a form without event.preventDefault() do?
Refreshes the page;
B/c of the action attribute (outdated)
JAVASCRIPT-FORMS
What property of a form element object contains all of the form’s controls?
elements property
JAVASCRIPT-FORMS
What property of a form control object gets and sets its value?
value
JAVASCRIPT-FORMS
What is one risk of writing a lot of code without checking to see if it works so far?
Realizing it doesn’t work
JAVASCRIPT-FORMS
What is an advantage of having your console open when writing a JavaScript program?
Can check to see if your code works as you’re working on it
DOM-CREATION
Does the document.createElement() method insert a new element into the page?
No, just creates a node;
You would still have to append it;
DOM-CREATION
How do you add an element as a child to another element?
appendChild( ) method
DOM-CREATION
What do you pass as the arguments to the element.setAttribute( ) method?
pass two stings;
setAttribute ( name of attribute, value we wanna set attribute to);
DOM-CREATION
What steps do you need to take in order to insert a new element into the page?
create an element, querySelect for parent, generate child element
Example:
var h1 =
document.createElement(‘h1’)
someDiv.appendChild(h1)
DOM-CREATION
What is the textContent property of an element object for?
To append text to a node and for looking at current text / to get or set text content
You can also check the text with textContent
DOM-CREATION Name two ways to set the class attribute of a DOM element.
setAttribute( );
Example:
setAttribute(‘className’, ‘classValues’)
class.Name;
Example:
element.className = ‘class’
DOM-CREATION
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
- saves a lot of time
- makes it reusable
DOM-EVENT-DELEGATION
What is the event.target?
event.target is
where the event originated from
DOM-EVENT-DELEGATION
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
DOM-EVENT-DELEGATION
What DOM element property tells you what type of element it is?
event.target.tagName
DOM-EVENT-DELEGATION
What does the element.closest() method take as its argument and what does it return?
The element.closest( ) method take as its argument… a CSS selector;
It matches it with closest ancestor (parent) element
DOM-EVENT-DELEGATION
How can you remove an element from the DOM?
call the remove method on the element we want removed
element.remove( )
DOM-EVENT-DELEGATION
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
addEventListener to the parent element;
use tagName method to check if you’re targeting the right element;
JAVASCRIPT-VIEW-SWAPPING
What is the event.target?
Where the event originated from
JAVASCRIPT-VIEW-SWAPPING
What is the affect of setting an element to display: none?
It will hide the element from the viewer
JAVASCRIPT-VIEW-SWAPPING
What does the element.matches() method take as an argument and what does it return?
The element.matches ( ) takes as an argument... css selectors and returns a boolean value
JAVASCRIPT-VIEW-SWAPPING
How can you retrieve the value of an element’s attribute?
In order to retrieve the value of an element’s attribute,
call it on the DOM element
JAVASCRIPT-VIEW-SWAPPING
At what steps of the solution would it be helpful to log things to the console?
Every Step
JAVASCRIPT-VIEW-SWAPPING
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 add an event listener for each individual element
JAVASCRIPT-VIEW-SWAPPING
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 to write many conditional statements
JAVASCRIPT-AND-JSON
What is JSON?
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
In summary;
JavaScript Object Notation and lets us store them that is not in memory space
JAVASCRIPT-AND-JSON
What are serialization and deserialization?
Converting a native object to a string so it can be transmitted across the network is called serialization;
Converting a string to a native object to be able to transmit is called deserialization
JAVASCRIPT-AND-JSON
Why are serialization and deserialization useful?
JSON exists as a string — useful when you want to transmit data across a network.
It needs to be converted to a native JavaScript object when you want to access the data.
Breaks down data to byte size and easy to transmit data and load data throughout the web and store data
JAVASCRIPT-AND-JSON
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify( )
JAVASCRIPT-AND-JSON
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse( )
JAVASCRIPT-LOCAL-STORAGE
How do you store data in localStorage?
localStorage.setItem( keyName , keyValue )
JAVASCRIPT-LOCAL-STORAGE
How do you retrieve data from localStorage?
localStorage.getItem( keyName )
JAVASCRIPT-LOCAL-STORAGE
What data type can localStorage save in the browser?
Strings
JAVASCRIPT-LOCAL-STORAGE
When does the ‘beforeunload’ event fire on the window object?
It is fired when the window, the document, and its resources are about to be unloaded.
JAVASCRIPT-CUSTOM-METHODS
What is a method?
A method is a function stored within a property of an object.
Two kinds:
Instance Methods - built-in tasks performed by an object instance;’
Static Methods - tasks that are called directly on an object;
JAVASCRIPT-CUSTOM-METHODS
How can you tell the difference between a method definition and a method call?
Method call is conceptualized as a message (the name of the method and its input parameters) being passed to the object for dispatch.
Method definition is conceptualized as the shorthand for a function assigned to the method’s name.
JAVASCRIPT-CUSTOM-METHODS
Describe method definition syntax (structure).
The method definition syntax (structure) may include: var object = { Property: function (anonymous or named) (parameters) { steps of the function } };
JAVASCRIPT-CUSTOM-METHODS
Describe method call syntax (structure).
The method call syntax (structure) may include:
The name of the method and its input parameters
Example:
- name of object. name of method(parameter)
JAVASCRIPT-CUSTOM-METHODS
How is a method different from any other function?
A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.
JAVASCRIPT-CUSTOM-METHODS
What is the defining characteristic of Object-Oriented Programming?
The defining characteristic of Object-Oriented Programming is:
objects can contain both data (as properties) and behavior (as methods).
JAVASCRIPT-CUSTOM-METHODS
What are the four “principles” of Object-Oriented Programming?
Four "principles" of Object-Oriented Programming: Abstraction, Encapsulation, Inheritance, Polymorphism
JAVASCRIPT-CUSTOM-METHODS
What is “abstraction”?
An “abstraction” is a way to interact with a system in a simplified, consistent fashion. Involves the usage of data types, modeling and usage of procedures, functions, or subroutines which represent a specific of implementing control flow in programs.
- Control Flow: is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated
JAVASCRIPT-CUSTOM-METHODS
What does API stand for?
Application Programming Interface (API)
- connection between computers or between computer programs
JAVASCRIPT-CUSTOM-METHODS
What is the purpose of an API?
The purpose of software API is to give programmers a way to interact with a system (and functionality) in a simplified, consistent fashion.
Involves connection between computers or between computer programs.
One purpose of APIs is to hide the internal details of how a system works, exposing only those parts a programmer will find useful and keeping them consistent even if the internal details later change.
JAVASCRIPT-THIS
What is this in JavaScript?
The object you’re working with currently. A variable whose value is the object you’re currently working with.
JAVASCRIPT-THIS
What does it mean to say that this is an “implicit parameter”?
Can use even though it is not explicitly declared in the function (present but not directly stated).
JAVASCRIPT-THIS
When is the value of this determined in a function; call time or definition time?
Call time
JAVASCRIPT-THIS
What does this refer to in the following code snippet?
this means nothing.. because it was never called
JAVASCRIPT-THIS
Given the above character object, what is the result of the following code snippet? Why?
“It’s a-me, Mario”
JAVASCRIPT-THIS
Given the above character object, what is the result of the following code snippet? Why?
“It’s a-me, undefined”
JAVASCRIPT-THIS
How can you tell what the value of this will be for a particular function or method definition?
We don’t know….
JAVASCRIPT-THIS
How can you tell what the value of this is be for a particular function or method call?
the object to the left of the dot
JAVASCRIPT-PROTOTYPES
What kind of inheritance does the JavaScript programming language use?
JavaScript includes a specific kind of inheritance known as prototype-based (or prototypal) inheritance where JavaScript objects give certain behaviors (methods) or data (properties) to other objects.
JAVASCRIPT-PROTOTYPES
What is a prototype in JavaScript?
Prototypes are the mechanism by which JavaScript objects inherit features from one another.
JAVASCRIPT-PROTOTYPES
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?
Through inheritance
JAVASCRIPT-PROTOTYPES
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
in the prototype
JAVASCRIPT-CONSTRUCTORS
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.
- creates a new blank object
- points to prototype of the function that made it
- this becomes whatever object that was just made
- returns that object if no other return is specified (which there shouldn’t be one)
JAVASCRIPT-CONSTRUCTORS
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype property
stores an object and object holds shared data and behavior
JAVASCRIPT-CONSTRUCTORS
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.
- left being variable (whatever you want to check) and right being what you want to check the instance of
JAVASCRIPT-TIMERS
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.
JAVASCRIPT-TIMERS
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?
setTimout( ) method
- method sets a timer which executes a function or specified piece of code once the timer expires
JAVASCRIPT-TIMERS
How can you set up a function to be called repeatedly without using a loop?
setInterval( )
- method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call
JAVASCRIPT-TIMERS
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
If delay parameter is omitted, a value of 0 is used, meaning execute “immediately”, or more accurately, the next event cycle.
JAVASCRIPT-TIMERS
What do setTimeout() and setInterval() return?
The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout( ).
JAVASCRIPT-AJAX
What is AJAX?
AJAX is a technique for loading data into part of a page without having to refresh the entire page.
JAVASCRIPT-AJAX
What does the AJAX acronym stand for?
Asynchronous Javascript And XML
JAVASCRIPT-AJAX
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
Browsers implement an object called XMLHttpRequest to handle Ajax requests.
Once a request has been made, the browser does not wait for a response from the server.
JAVASCRIPT-AJAX
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
- When the server has finished responding to the request, the browser will fire an event (just like it can fire an event when a page has finished loading). This event can be used to trigger a JavaScript function that will process the data and incorporate it into one part of the page (without affecting the rest of the page).
JAVASCRIPT-AJAX
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototypal inheritance