JavaScript Flashcards Preview

Web Development > JavaScript > Flashcards

Flashcards in JavaScript Deck (151)
Loading flashcards...
1
Q

What is the purpose of variables?

A

The purpose of variables is to store value.

Why does this help? Variables can change & it exists for a longer time.(data permanence)

2
Q

How do you declare a variable?

A

You declare a variable by using the var keyword and giving the variable a name.

3
Q

How do you initialize (assign a value to) a variable?

A

You initialize (assign a value to) a variable with the = operator.

4
Q

What characters are allowed in variable names?

A

Letters, numbers, dollar sign($), and underscore(_).

Note: dash(-) or period( . ) or question-mark(?) are not allowed & Numbers cannot go first.

5
Q

What does it mean to say that variable names are “case sensitive”?

A

When it is said that variable names are “case sensitive” it means that capitalization matters.

Ex: String and string are not the same.

6
Q

What is the purpose of a string?

A

The purpose of a string is to store text.

7
Q

What is the purpose of a number?

A

The purpose of a number is to use it for math and calculations.

8
Q

What is the purpose of a boolean?

A

The purpose of a boolean is to store if something is true or false. Booleans exist for decision making.

9
Q

What does the = operator mean in JavaScript?

A

The = operator in JavaScript means that we are assigning a value to something.

10
Q

How do you update the value of a variable?

A

You update the value of a variable by assigning a new value to the variable.

Note: You don’t need the var keyword to assign a new value to an existing variable.

11
Q

What is the difference between null and undefined?

A

Null: Primitive value that represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. Null always need to be assigned to something.

Undefined: Primitive value automatically assigned to variables that have only been declared. Also assigned to formal arguments where there are no actual arguments.

12
Q

What data-type is returned by an arithmetic operation?

A

A Number value is returned by an arithmetic operation.

13
Q

What is string concatenation?

A

String concatenation is adding two or more strings to make a longer string.

14
Q

What purpose(s) does the + plus operator serve in JavaScript?

A

The + plus operator adds numbers and concatenates strings in JavaScript.

15
Q

What data type is returned by comparing two values (< , >, ===, etc)?

A

A Boolean value is returned by comparing two values (< , >, ===, etc).

16
Q

What does the += “plus-equals” operator do?

A

A value on the right-side will be added to the variable, and the variable will then be updated to that new value.

17
Q

What are objects used for?

A

Objects are used to group together a set of properties and methods to create a model of something.

18
Q

What are object properties?

A

Object properties are variables glued to that object.

19
Q

Describe object literal notation.

A
var hotel = {
name: 'Cosmopolitan',
rooms: 500,
booked: 300,
checkAvailability: function( ) {
    return this.rooms - this.booked;
  }
};
20
Q

How do you remove a property from an object?

A

Removing a property from an object using the dot notation:
delete objectname.propertyname.

Removing a property from an object using the bracket notation:
delete objectname[‘propertyname’].

21
Q

What are the two ways to get or update the value of a property?

A

Updating/getting a property from an object using the dot notation: variablename.propertyname.

Updating/getting a property from an object using the bracket notation: variablename[‘propertyname’].

22
Q

What are arrays used for?

A

Arrays are used to store a list of data with number indexes.

23
Q

Describe array literal notation.

A

colors = [‘white’, ‘blue’, ‘red’]

24
Q

How are arrays different from “plain” objects?

A

Arrays have orders to them unlike “plain” objects. Arrays will repair itself if a data is deleted.

25
Q

What number represents the first index of an array?

A

0 represents the first index of an array.

26
Q

What is the length property of an array?

A

The length property of an array tells us the number of data stored in an array.

27
Q

How do you calculate the last index of an array?

A

We calculate the last index of an array by:

arrayname.length - 1

28
Q

What is a function in JavaScript?

A

A function in JavaScript is giving a name to some amount of code so that it can be reused and also read easier.

29
Q

Describe the parts of a function definition.

A
function functionName (parameters) {
    return
}
30
Q

Describe the parts of a function call.

A

functionName (arguments)

Note: Arguments don’t have to be there.

31
Q

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

function definition - name of data get passed as parameters.

function call - actual values get passed as arguments.

32
Q

Why are function parameters useful?

A

It functions as a placeholder.

33
Q

Why are function parameters useful?

A

Parameters are useful because they function as placeholders.

Adds reusability and adaptability.

34
Q

What two effects does a return statement have on the behavior of a function?

A
  1. Causes the function to return a value we can use in our program.
  2. Prevents any more code in the function’s code block from being run.
35
Q

Why do we log things to the console?

A

We log things to the console for debugging purposes.

36
Q

What is a method?

A

A method is a function which is a property of an object.

37
Q

How is a method different from any other function?

A

A function becomes a method once it is passed in as a property for an object. Method has to say where it is coming from.

38
Q

How do you remove the last element from an array?

A

You can remove the last element from an array by using the pop( ) method.

39
Q

How do you round a number down to the nearest integer?

A

You can round a number down to the nearest integer by using the Math.floor( ) method.

40
Q

How do you generate a random number?

A

You can generate a random number in a range by:

Ex: Math.floor(Math.random( ) * 10);

41
Q

How do you delete an element from an array?

A

You can delete an element from an array by using the: pop( ) method, shift( ) method, and/or splice( ) method.

42
Q

How do you append an element to an array?

A

You can add an element to an array by using the: push( ) method, unshift( ) method, and or splice( ).

43
Q

How do you break a string up into an array?

A

You can break a string up into an array by using the split( ) method.

44
Q

Do string methods change the original string? How would you check if you weren’t sure?

A

No. You can check by using console.log ( ).

45
Q

Roughly how many string methods are there according to the MDN Web docs?

A

A lot. Learn it when you need it.

46
Q

Is the return value of a function or method useful in every situation?

A

No.

The push ( ), unshift ( ), pop( ), shift( ), and splice( ) are good examples for when you are solely just trying to manipulate the array.

47
Q

Roughly how many array methods are there according to the MDN Web docs?

A

A lot. Learn it when you need it.

48
Q

Give 6 examples of comparison operators.

A

==, !=, ===, !==, < , <= , >, and >=

49
Q

What data type do comparison expressions evaluate to?

A

Comparison expressions evaluate to Booleans.

50
Q

What is the purpose of an if statement?

A

The purpose of an if statement is for decision making.

51
Q

Is else required in order to use an if statement?

A

No.

52
Q

Describe the syntax (structure) of an if statement.

A

if ( condition ) {

}

53
Q

What are the three logical operators?

A

&&, | |, and !.

54
Q

How do you compare two different expressions in the same condition?

A

You compare two different expressions in the same condition by using logical && or logical | | operator.

55
Q

What is the purpose of a loop?

A

The purpose of a loop is to offer a quick and easy way to do something repeatedly.

56
Q

What is the purpose of a condition expression in a loop?

A

The purpose of a condition expression in a loop is to test if the loop-code-block should be executed or not. (To keep going or not)

57
Q

What does “iteration” mean in the context of loops?

A

“Iteration” in the context of loops means a cycle of the loop.

58
Q

When does the condition expression of a while loop get evaluated?

A

The condition expression of a while loop gets evaluated in the beginning.

59
Q

When does the initialization expression of a for loop get evaluated?

A

The initialization expression of a for loop gets evaluated in the beginning.
(initialization = before anything)

60
Q

When does the condition expression of a for loop get evaluated?

A

The condition expression of a for loop gets evaluated before each iteration.
(condition = before each iteration)

61
Q

When does the final expression of a for loop get evaluated?

A

The final expression of for loop gets evaluated after the loop-code-block was executed.
(final expression = after each iteration)

62
Q

Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

The “break” keyword.

63
Q

What does the ++ increment operator do?

A

The ++ increment operators increments by 1.

64
Q

How do you iterate through the keys of an object?

A

You can iterate through the keys of an object by using the for…in loop.

65
Q

What event is fired when a user places their cursor in a form control?

A

focus

66
Q

What event is fired when a user’s cursor leaves a form control?

A

blur

67
Q

What event is fired as a user changes the value of a form control?

A

input

68
Q

What event is fired when a user clicks the “submit” button within a ?

A

submit

69
Q

What does the event.preventDefault( method do?

A

Prevents the page from doing what it usually does.

70
Q

What does submitting a form without event.preventDefault( ) do?

A

Sends the data of key value pair to URL. (Query String) The data gets sent back to the current page.

71
Q

What property of a form element object contains all of the form’s controls.

A

elements

72
Q

What property of form a control object gets and sets its value?

A

value

73
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

Harder to debug.

74
Q

What is an advantage of having your console open when writing a JavaScript program?

A

Easier to debug.

75
Q

What is the event.target?

A

It is the dom object of the element where the event was fired.

76
Q

What is the affect of setting an element to display: none?

A

It hides the content.

77
Q

What does the element.matches( ) method take as an argument and what does it return?

A

It takes a CSS selector as an argument and returns a boolean.

78
Q

How can you retrieve the value of an element’s attribute?

A

getAttribute( )

79
Q

At what steps of the solution would it be helpful to log things to the console?

A

Almost at every step.

80
Q

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?

A

We would need event listeners for each of the tabs.

81
Q

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?

A

We need to check through each element individually.

82
Q

What is JSON?

A

JSON is a text-based data format following JavaScript object syntax.

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.

83
Q

What are serialization and deserialization?

A

Serialization: a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage.

Deserialization: a process of fetching a stream of bytes from a network or persistence storage and convert it back to the Object with the same state.

84
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

JSON.stringify( )

85
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

JSON.parse( )

86
Q

How do you store data in localStorage?

A

localStorage.setItem(‘key’, ‘value’ )

87
Q

How do you retrieve data from localStorage?

A

localStorage.getItem(‘key’)

88
Q

What data type can localStorage save in the browser?

A

JSON strings

89
Q

When does the ‘beforeunload’ event fire on the window object?

A

Before the window unloads.

90
Q

What is a method?

A

A method is a function which is a property of an object.

91
Q

How can you tell the difference between a method definition and a method call?

A

Method definition is a function that is a value of object:

Ex: Property: function ( )

Method call is property of object:

Ex: Object.method( )

92
Q

Describe method definition syntax (structure).

A

Ex:

var calculator = {
  add: function (x, y) {
    return x + y;
  },
93
Q

Describe method call syntax (structure).

A

object.method( )

94
Q

How is a method different from any other function?

A

Methods are functions attached to objects.

95
Q

What is the defining characteristic of Object-Oriented Programming?

A

Objects can contain both data (as properties) and behavior (as methods).

96
Q

What is “abstraction”?

A

Being able to work with (possibly) complex things in simple ways.

Example: Automatic Car Transmission

97
Q

What does API stand for?

A

Application Programming Interface

98
Q

What is the purpose of an API?

A

To give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

99
Q

What is ‘this’ in JavaScript?

A

‘this’ is an implicit parameter of all JavaScript functions.

100
Q

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);
  }
};
A

‘this’ refers to the object ‘character’.

101
Q

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);
  }
};
A

Since the method is not being used, ‘this’ does not have anything inside.

102
Q

Given the below character object:

var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};

What is the result of the following code snippet? Why?

character.greet();

A

The result will be ‘It’s a me, Mario!”

Because the value of the parameter greet is ‘It's-a-me, ‘ + this.firstName + ‘!’;.

103
Q

Given the below character object:

var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};

What is the result of the following code snippet? Why?

var hello = character.greet;
hello();
A

The result will be ‘It’s a me, undefined!”

Because the greet method hasn’t been called.

The value of ‘this’ is still window.
The window object does not have a property firstName.

104
Q

How can you tell what the value of this will be for a particular function or method definition?

A

You can’t. If you cannot see the function being called, then you do not know what the value of ‘this’ will be.

105
Q

How can you tell what the value of ‘this’ is for a particular function or method call?

A

The value of ‘this’ can be recognized as “the object to the left of the dot” when the function is called (as a method).

106
Q

When is the value of ‘this’ determined in a function; call time or definition time?

A

call time

107
Q

What kind of inheritance does the JavaScript programming language use?

A

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.

Prototypal inheritance in JavaScript is a fairly simple concept (especially compared to ‘this’). The ultimate goal is to move properties and methods that we’d like to reuse into a “prototype” object and then tell other objects to simply delegate (verb) to that “prototype” object when they aren’t able to perform the required tasks themselves.

108
Q

What is a prototype in JavaScript?

A

JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be used by other objects.

109
Q

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?

A

The methods exist in their prototype object.

110
Q

If an object does not have its own property or method by a given key, where does JavaScript look for it?

A

In the __proto__ object.

111
Q

What does the new operator do?

A
  1. Creates a blank, plain JavaScript object.
  2. Adds a property to the new object (__proto__) that links to the constructor function’s prototype object.

Note: Properties/objects added to the construction function prototype are therefore accessible to all instances created from the constructor function (using new).

  1. Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
  2. Returns this if the function doesn’t return an object.
112
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

.prototype

Example:

Student.prototype.getFullName = function () {
  var fullName = this.firstName + ' ' + this.lastName;
  return fullName;
};
113
Q

What does the instanceof operator do?

A

Checks if an instance is an instance of a specific object or not.

Returns a boolean.

114
Q

What is a “callback” function?

A

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.

115
Q

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?

A

using setTimeout( )

116
Q

How can you set up a function to be called repeatedly without using a loop?

A

using setInterval( )

117
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0

118
Q

What do setTimeout() and setInterval() return?

A

It returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval().

interval ID is an integer.

119
Q

What is AJAX?

A

AJAX is a technique for loading data into part of a page without having to refresh the entire page.

Use XMLHttpRequest( )

120
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript And XML

121
Q

Which object is built into the browser for making HTTP requests in JavaScript?

A

XMLHttpRequest ( )

122
Q

What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

load

123
Q

Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

It is in the prototype.

Both branching off of the same prototype.

124
Q

What is a code block? What are some examples of a code block?

A

Code in-between { } to be executed together that are statements.

Examples of a code block are function code blocks, conditional code blocks, loop code blocks.

125
Q

What does block scope mean?

A

In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.

126
Q

What is the scope of a variable declared with const or let?

A

block scope

127
Q

What is the difference between let and const?

A

You can’t reassign values declared in const.

128
Q

Why is it possible to .push() a new value into a const variable that points to an Array?

A

Can’t reassign but we can manipulate the value.

129
Q

How should you decide on which type of declaration to use?

A

If the value is going to be reassigned, use let.
If not, use const.

Use const everywhere unless you can’t.

130
Q

What is the syntax for writing a template literal?

A

${variable}

131
Q

What is “string interpolation”?

A

The ability to substitute part of the string for the values of variables or expressions.

132
Q

What is destructuring, conceptually?

A

Destructuring broadly means extracting data from arrays or objects. Using destructuring, we can break a complex array or object into smaller parts. Destructuring also offers us the ability to extract multiple data at once from an object or array. It also provides the ability to set a default value of the property if it’s already not set.

133
Q

What is the syntax for Object destructuring?

A

var obj {
}

const/let {keyName: variableName} = obj

or

const/let {keyName/varibleName} = obj
if the the key name and variable name are the same.

134
Q

What is the syntax for Array destructuring?

A

var array [
]

const/let [arrayValue] = array
if only grabbing first value of array and putting it into a variable.

or

const/let {index:variableName} if using object destructuring.

135
Q

How can you tell the difference between destructuring and creating Object/Array literals?

A

The { } and [ ] followed with the name of the object/array after the =.

Creating:
const/let array = [ ]

Destructuring:
const/let [arrayValue] = arrayName

136
Q

What is the syntax for defining an arrow function?

A
const greet = (who) => {
  return `Hello, ${who}!`;
}
137
Q

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

It has an implicit return but the statement must be in one-line. (The right side of the arrow is the expression)

138
Q

How is the value of ‘this’ determined within an arrow function?

A

In regular functions the ‘this’ keyword represented the object that called the function, which could be the window, the document, a button or whatever. ‘this’ is determined at function call.

With arrow functions the this keyword always represents the object that defined the arrow function. ‘this’ is determined at function definition. Go up one level and search for ‘this’.

139
Q

What is Array.prototype.filter useful for?

A

To make a new array from specific elements of an existing array.

140
Q

What is Array.prototype.map useful for?

A

To make a new array after the elements have been altered.

141
Q

What is Array.prototype.reduce useful for?

A

Take all elements of an array and combine them somehow.

142
Q

What is “syntactic sugar”?

A

syntax within a programming language that is designed to make things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

143
Q

What is the “typeof” an ES6 class?

A

function

144
Q

Describe ES6 class syntax.

A
class Person {
    constructor(name) {
        this.name = name;
    }
    getName( ) {
        return this.name;
    }
}
145
Q

What does fetch() return?

A

a promise. The promise is for a response at all.

146
Q

What is the default request method used by fetch()?

A

GET

147
Q

How do you specify the request method (GET, POST, etc.) when calling fetch?

A

Pass the method as the second argument.

148
Q

What must the return value of myFunction be if the following expression is possible?

myFunction( )( );

A

Must be a function.

Second () is calling the return of myFunction.

149
Q

What does this code do?

const wrap = value => ( ) => value;

A

It is assigning an anonymous function with a single parameter “value” to the const variable wrap. And in the code-block, another anonymous function is defined with the parameter value.

The code assigns the value of the parameter to const variable wrap.

150
Q

In JavaScript, when is a function’s scope determined; when it is called or when it is defined?

A

When it is defined.

151
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

closures / lexical environment