Javascript Flashcards
what is the purpose of variables?
to declare a value.
to store info to be referenced
how do you declare a variable?
using var, let, const keyword
how do you initialize ( assign a value to ) a variable?
add an equal sign and put the value
what characters are allowed in variable names?
No other characters are permitted in the variable name. Specifically, spaces are not permitted in the variable names, as variable name must be a single word. Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.
what does it mean to say that variable names are “case sensitive”?
Uppercase and lowercase letters are treated as distinct
what is the purpose of a string?
to store or indicate text value
what is the purpose of a number?
to store or indicate numerical value
what is the purpose of a boolean?
to store or indicate true/false value
what does the = operator mean in javascript?
assigning a value
how do you update the value of a variable?
by assigning a different value that you want
what is the difference between null and undefined?
a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.
undefined is that, there’s no value in it.
why is it a good habit to include “labels” when you log values to the browser console?
to make debugging easier.
Five examples of javascript primitives
string, number, boolean, null, undefined
what data type is returned by an arithmetic operation?
- numeric data type
what is string concatenation?
- connecting strings by using +. combining strings.
- strings are immutable in javascript. (not mutable, it cannot change)
what purpose does the + plus operator serve?
concatenating strings or add numeric values with arithmetic operators.
what data type is returned by comparing two values?
boolean
what does the += plus equals operator do?
add value to the variable and store the value in the same variable
what are objects used for?
JavaScript objects are containers for named values, called properties and methods.
what are object properties?
association between key and value
Describe object literal notation
The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array.
- opening and closing curly brases
how do you remove a property from an object?
by using the delete keyword
what are the two ways to get or update the value of a property?
adding keys by using dot notation or add inside the object directly.
what are arrays used for ?
it is used for storing different elements
describe array literal notation
array literal notation is where you define a new array using just empty brackets.
how are arrays different from “plain” objects?
you can get values by indicating the index.?
- doesnt need a name key.
- have a property name length and is automatically updated when adding a new value.
- list .
what number represents the first index of array?
0
what is the length property of an array?
get the total number of values
how do you calculate the last index of an array?
total length - 1
what is a function in javascript?
a block of code designed to perform a particular task.
describe the parts of a function definition
the function keyword, an optional name, a comma-separated list of zero or more parameters, surrounded by parentheses. The start of the function’s code block, an optional return statement. the end of the function’s code block.
describe the parts of a function call
write the function name and parentheses next to it. pass an argument to the function
describe the parts of a function call
write the function name and parentheses next to it. pass an argument to it?
what are the differences between a function call and a function definition?
calling a function means executing it.
what is the difference between a parameter and an argument?
Parameter is variable in the declaration of function.
Argument is the actual value of this variable that gets passed to function.
why are function parameters useful?
A function can take parameters which are just values you supply to the function so that the function can do something utilising those values.
reusing values for other purposes?
what two effects does a return statement have on the behavior of a function?
The return statement ends function execution and specifies a value to be returned to the function caller
why do we log things to the console.?
to see the result value and for debugging
what is the method?
method is a function which is a property of an object
how is a method different from any other function?
function themselves are objects, so, in that context, a method is actually an object reference to a function.
How do you remove the last element from an array?
use the pop() method.
how do you round a number down to the nearest integer?
Math.floor() bring it down to the nearest integer.
how do you generate a random number?
Math.random()
How do you delete an element from an array?
splice(start, deleteCount, item1)
how do you append an element to an array?
push()
how do you break a string up into an array?
split()
do string methods change the original string? how would you check if you weren’t sure?
All string methods return a new string. They don’t modify the original string. Formally said: Strings are immutable: Strings cannot be changed, only replaced.
roughly how many string methods are there according to the MDN web docs?
45-50 methods.
is the return value of a function or method useful in every situation?
sometimes. push() method is just to add an element to the array
roughly how many array methods are there according to the MDN web docs?
many many
what three letter acronym should you always include in your google search about a javascript method or css property?
MDN
Give six examples of comparison operators.
, ===, ==, >=, <=, !==
what data type do comparison expressions evaluate to?
boolean, true or false
what is the purpose of an ‘if’ statement?
to declare a condition within the function
Is ‘else’ required in order to use an ‘if’ statement?
not always.
describe the syntax of an ‘if’ statement
if (condition) {
}
what are the three logical operators?
and / or / not
how do you compare two different expressions in the same condition?
adding add (&&) or OR (||) with two conditions
what is the purpose of a loop?
Loops are used in JavaScript to perform repeated tasks based on a condition.
what is the purpose of a condition expression in a loop?
Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run
what does “iteration” mean in the context of loops?
a step where some action is repeated
when does the condition expression of a ‘while’ loop get evaluated?
An expression evaluated before each pass through the loop.
If this condition evaluates to true, statement is executed.
When condition evaluates to false, execution continues with the statement after the while loop.
when does the initialization expression of a for loop get evaluated?
start point of the loop.
when does the condition expression of a for loop get evaluated?
after the initializtion and the condition
when does the final expression of a for loop get evaluate?
after the code block is done and after the next condition is executed?
Besides the return statement, which exits its entire function block?
break;
what does the ++ increment operator do?
adds number one, re-assigns and substitutes in its value.
how do you iterate through the keys of an object?
use the ‘for in ‘ loops
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 ?
submit event
what does the event.preventDefault() method do?
it tells the user agent that if the event does not get explicitly handed, its default action should not be taken as it normally would be. (prevents the default action in event - example: refreshing the form)
necessary for every submit event in forms
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?
value property
what is one risk of writing a lot of code without checking to see if it works so far?
hard to debug
what is an advantage of having your console open when writing a JavaScript program?
you can check the values one at a time.
what is the event.target?
the element that triggered the event
What is the affect of setting an element to display: none?
display: none; When you set the value of display to none, the affected element will disappear. This means the element will no longer take up any space on the web page.
no longer visible and removed from the document flow
What does the element.matches() method take as an argument and what does it return?
You invoke the matches() method on the element itself (the element has to be an HTML element), and like querySelector / querySelectorAll , you pass the selector as a string argument. The method returns true for a match, and false otherwise.
How can you retrieve the value of an element’s attribute?
Get the value of an attribute of a specified element by calling the getAttribute() method on the element. The getAttribute() returns null if the attribute does not exist.
At what steps of the solution would it be helpful to log things to the console?
all steps for the solution
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?
Individually apply each event handlers for each element
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?
we would have to add each if statement for every element
what is JSON?
JavaScript Object Notation
a standard text-based format for representing structured data based on Javascript object syntax. It is commonly used for transmitting data in web applications (sending some data from the server to the client, so it can be displayed on a web page)
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 a disk or send it over the network
Deserialization is the reverse process.
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
why are serialization and deserialization useful?
Its main purpose is to save the state of an object in order to be able to recreate it when needed.
transferring the bytes. transfer through network and save it to hard-drive
How do you store data in ‘localStorage’?
localStorage.setItem(key, value);
How do you retrieve data from localStorage?
localStorage.getItem(key)
what data type can ‘localStorage’ save in the browser?
string type
when does the ‘beforeunload’ event fire on the window object?
when the window, the document and its resources are about to be unloaded.
the document is still visible and the event is still cancelable at this point
what is a method?
a method is a function which is a property of an object.
Instance methods: built-in tasks performed by an object instance
Static methods: tasks that are called directly on an object constructor
How can you tell the difference between a method definition and a method call?
declare and define are the same, and they mean when you write all the code for your function. At that point the function just sits there doing nothing. call is when you tell the JavaScript interpreter to run the code in your function.
Describe method definition syntax (structure).
var something = { add: function(x, y) { return result } }
Describe method call syntax(structure)
something.add()
How is a method different from any other function?
A function is independent, whereas a method is a function linked with an object. Explicit data is passed on to a function, whereas a method completely passes the object on which it was called in the program. A method is Object-oriented programming while a function has standalone functionality.
what are the four principles of OOP?
Abstraction, Encapsulation, Inheritance, Polymorphism
what is the defining characterisitic of OOP?
objects can contain both data (as properties) and behavior (as methods).
What is abstraction?
being able to work with complex things in simple ways
what does API stand for?
Application Programming Interface(API)
what is the purpose of an API?
API stands for Application Programming Interface. The purpose of API is to establish communication between two software. (SOFTWARE ABSTRACTION)
For example, let’s say you have typed in some information on your phone. Now, this input information goes to the server, and the server process the data. After processing the data it sends the data to your phone. Your phone interprets the data and interprets the data to you.
All this happens is through API. So this is the use case of API. API are just codes that help to communicate between several programming g interfaces.
https://www.quora.com/What-is-the-purpose-of-API
what is ‘this’ in javascript?
In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called).
The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.
what does it mean to say that this is an “implicit parameter”?
this is different from explicit parameters: it’s 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.
you dont have to define it
‘when’ is the value of ‘this’ determined in a function?
call time
-> just like explicit parameters, the value of this is determined when the function is called, not when it is defined. By default, when you call a function, it’s this will be given the value of the global window object.
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); } };
character variable -> no. becuz its not being called. -> so its nothing. (parameters do not have a value, it doesn’t exist)
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
the greeting message. “Its me, mario” - why?
becuz we’re calling the greet method of the character object
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
undefined. window does not have a firstName property.
How can you tell what the value of this will be for a particular function or method definition?
you cant. there’s no way cuz its not being used yet.
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).
What kind of inheritance does the JavaScript programming language use?
JavaScript includes a specific kind of inheritance known as prototype-based (or prototypal) inheritance. JavaScript objects give certain behaviors (methods) or data (properties) to other objects.
what is a prototype in JavaScript?
The prototype is an object that is associated with every functions and objects by default in JavaScript, where function’s prototype property is accessible and modifiable and object’s prototype property (aka attribute) is not visible. … Every function includes prototype object by default.
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?
make a prototype and set the prototype to the object.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
proto
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 property
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()
How can you set up a function to be called repeatedly without using a loop?
setInterval() function
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
value of zero meaning immediately.
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(); this value can be passed to clearInterval() to cancel the interval.
what is ajax?
Asynchronous JavaScript And XML.
web application that sends and receive information without refreshing the page.
it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files. AJAX’s most appealing characteristic is its “asynchronous” nature, which means it can communicate with the server, exchange data, and update the page without having to refresh the 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
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Prototype inheritance.
Inherits properties of XMLHttpRequestEventTarget and of EventTarget
Element, and its children, as well as Document and Window, are the most common event targets, but other objects can be event targets, too. For example XMLHttpRequest, AudioNode, and AudioContext are also event targets.
What is Array.prototype.filter useful for?
Array.prototype.filter() The filter() method creates a new array with all elements that pass the test implemented by the provided function.
What is Array.prototype.map useful for?
map() The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
What is Array.prototype.reduce useful for?
Array.prototype.reduce() The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
What must the return value of myFunction be if the following expression is possible?
myFunction()();
function
What does this code do? const wrap = value => () => value;
returns value function
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
when it’s defined (lexical scope)
What allows JavaScript functions to “remember” values from their surroundings?
closures