JavaScript Flashcards

1
Q

What is the purpose of variables?

A

To store data(information) for a script.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare a variable?

A

You must declare the variable with a name. Providing a keyword (var) to declare a variable with a name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

You use the equal sign = (assignment)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What characters are allowed in variable names?

A

Letters, numbers (if it’s not the first character), $ or _

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

Lowercase and uppercase letters are different.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of a string?

A

To hold a sequence of characters or text.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of a number?

A

For math and calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of a boolean?

A

Conditional data type to determine if something is true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the = operator mean in JavaScript?

A

It assigns things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you update the value of a variable?

A

Use the variable name and assign it a new value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between null and undefined?

A

null is an intentional absence of value. Must be assigned.
undefined – javaScript says there’s nothing there.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it a good habit to include “labels” when you log values to the browser console?

A

There’s no context as to what it is or where it came from without labels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give five examples of JavaScript primitives.

A

String, number, Boolean, null, undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What data type is returned by an arithmetic operation?

A

numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is string concatenation?

A

Combines multiple strings together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

Addition adds one value to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

Adds the value to the right of the operand to a variable and assigns the result to the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are objects used for?

A

Group together a set of variables or properties and values to describe something. A collection of variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are object properties?

A

Properties or variables that have a value within an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Describe object literal notation.

A

Assign an object literal to a variable. Within the object are names of properties and their corresponding values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you remove a property from an object?

A

Delete operator Ex. delete pet.name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

Dot notation or bracket notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are arrays used for?

A

Arrays store a list of values. Values can then be accessed if they are in a numbered list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Describe array literal notation.

A

Create an array and give it a name. the values are assigned to the array inside a pair of brackets and each value is separated by a comma.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How are arrays different from “plain” objects?

A

Arrays have a length property. Arrays have an order. Objects you create properties and assign them a value. Arrays you would normally assign a method to add data to an array. arr.push() can also arr[0] = “Something”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What number represents the first index of an array?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the length property of an array?

A

It tells you how many items are in an array. .length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you calculate the last index of an array?

A

Length property -1. .length - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is a function in JavaScript

A

It’s a tool used to write code for reuse throughout a program, giving a name to a handful of code statements to make it easier to read and makes code dynamic to handle many situations. It takes in data and spits out data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Describe the parts of a function definition.

A

Function keyword to begin the creation of a function.
An optional name of the function.
A list of zero or more parameters within parentheses.
The start of the functions code block indicated by an opening curly brace {
An optional return statement.
End of the function code block indicated by closing curl braces }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function call.

A

The functions name.
List of zero or more arguments within parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

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

A

Function definition has a code block and a keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the difference between a parameter and an argument?

A

We declare or define parameters during the definition of the function. It is a placeholder variable whose value is not known until we call the function and pas san argument. An argument is the actual value that will be passed through to the parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Why are function parameters useful?

A

Parameters are placeholders for variables. When we call a function we can pass whatever arguments we want to the parameter. We can change the output by calling the function with the arguments and the parameters will change within the codeblock to whatever we set the argument to. Parameters allow a function to perform tasks without knowing the specific input values ahead of time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

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

A

Causes the function to produce a value we can use in our program.

Prevents any more code in the function’s code block from being run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Give 6 examples of comparison operators.

A

> < >= <= === !==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What data type do comparison expressions evaluate to?

A

boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is the purpose of an if statement?

A

Evaluates or checks a condition. If the condition is true, the statements in the following codeblock are executed. To make a choice.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Is else required in order to use an if statement?

A

No. If you require code to run only when the statement returns true an else statement is not needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Is else required in order to use an if statement?

A

No. If you require code to run only when the statement returns true an else statement is not needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Describe the syntax (structure) of an if statement.

A

Keyword if
Condition (score>=50)
Opening a curly brace for declaration block
the code to execute.
Closing curly brace for the declaration block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What are the three logical operators?

A

&& (logical and), || (logical or), ! (logical not)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

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

A

By using logical operators.
(Expression 1) logical operator (expression 2)
(1<2) || (3>4) > true

45
Q

Why do we log things to the console?

A

To see if there are any bugs in the code. The browser will print errors and warnings as they occur in the JS code.

46
Q

What is a method?

A

A function which is a property of an object. There are two kinds:
Instance methods which are built-in tasks performed by an object instance.
Static methods: which are tasks that are called directly on an object constructor.

47
Q

How is a method different from any other function?

A

methods are associated with objects.

48
Q

How do you remove the last element from an array?

A

The pop() method.

49
Q

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

A

Math.floor() Floor method of the Math object.

50
Q

How do you generate a random number?

A

Math.random() Random method of the Math object. Random value between 0 and 1. As a percentage. You can multiply to something.

51
Q

How do you delete an element from an array?

A

Splice() method pass through the array index.

52
Q

How do you append an element to an array?

A

The push() method.

53
Q

How do you break a string up into an array?

A

The split() method.

54
Q

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

A

They don’t change the original string. If you wanted to change them you would have to replace them. Console.log

55
Q

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

A

Not always. ** look into.

56
Q

What is the purpose of a loop?

A

To repeatedly run a block of code until a certain condition is met. Returns false.

57
Q

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

A

It determines when the loop will stop.

58
Q

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

A

One execution of the loops conditional code block.

59
Q

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

A

Before each iteration.

60
Q

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

A

That is the first thing the for loop does.

61
Q

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

A

After the initialization expression and after the final expression. Or before the iteration of the codeblock.

62
Q

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

A

After each iteration of the codeblock.

63
Q

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

A

Break. Causes the termination of the loop and tells the interpreter to go onto the next statement outside of code outside of the loop.

64
Q

What does the ++ increment operator do? Arithmetic

A

Adds 1 to its operand. Increases the value of the variable by 1.

65
Q

How do you iterate through the keys of an object?

A

For in loop.

66
Q

What is JSON?

A

JavaScript Object Notation
A standard text-based format for representing structured data based on Javascript object syntax.

67
Q

What are serialization and deserialization?

A

Serialization: converting data(object) to a string
Deserialization: converting string to object

68
Q

Why are serialization and deserialization useful?

A

Serialization allows data to be stored in memory and makes it easier to transfer data across a network.
Deserialization makes it so that data is easier to interact with by turning a bytes into an object in memory.

69
Q

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

A

JSON.stringify(). pass through the data as an argument.

70
Q

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

A

JSON.parse(). Pass through the string as an argument.

71
Q

How do you store data in localStorage?

A

localStorage.setItem() method.

72
Q

How do you retrieve data from localStorage?

A

localStorage.getItem() method.

73
Q

What data type can localStorage save in the browser?

A

‘string’ needs serialized data.

74
Q

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

A

Before the page unloads.

75
Q

What is a method?

A

A function that is a property of an object.

76
Q

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

A

A method definition has the keyword function and { } for the function code block. A method call is what we see. Object dot notation method name and ( ).

77
Q

Describe method definition syntax (structure).

A

{property: function methodName ([optional parameters]) {code block},}

78
Q

Describe method call syntax (structure).

A

Object.methodName( )

79
Q

How is a method different from any other function?

A

A method needs dot notation or bracket notation. Also a method belongs to an object as a property of the object.

80
Q

What is the defining characteristic of Object-Oriented Programming?

A

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

81
Q

What are the four “principles” of Object-Oriented Programming?

A

Abstraction, encapsulation, inheritance, polymorphism

82
Q

What is “abstraction”?

A

It is a process that enables a user to work with complex things in simple ways.

83
Q

What does API stand for?

A

Application programming interface

84
Q

What is the purpose of an API?

A

Selection of tools (set of code features such as methods, properties, events, and URLs) to make it easier for developers to interact with software.

85
Q

What is this in JavaScript?

A

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

86
Q

What does it mean to say that this is an “implicit parameter”?

A

It is available in a function’s code block even though it was never included in the function’s parameters list or declared with var.

87
Q

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

A

Call.

88
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

Do not know yet. The value of ‘this’ can only be determined during call time (not during a function definition)

89
Q

Given the above character object, what is the result of the following code snippet? Why?
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
character.greet();

A

Result = “it’s-a-me, Mario!” because ‘this’ refers to the object being called. The object firstName property has the value of Mario.

90
Q

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

A

Result = “it’s-a-me, undefined!” because ‘this’ refers to the window (hello is not a property of an object, therefore default object would be the window object). Window object does not have the property frisrtName so therefore, ‘this.firstNAme” has the value of undefined.

91
Q

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

A

You can’t. the value of ‘this’ is determined at call time.

92
Q

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

A

By looking to the left of the dot. (the object) if no dot it’s the window.

93
Q

What kind of inheritance does the JavaScript programming language use?

A

JS uses prototype-based inheritance

94
Q

What is a prototype in JavaScript?

A

Exiting inbuilt functionality in javascript. An object that contains properties and methods that can be used by other objects.

95
Q

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?

A

Due to JS prototypes; models that was created that contain these methods.

96
Q

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

A

From the object’s prototype, if it’s not there then object’s object’s prototype.

97
Q

What does the new operator do?

A

Creates a blank, plain Javascript object
Links the newly created object to another object by setting the other object as its parent prototype;
Passes the newly created object from step 1 as the this context;
Returns this if the function doesn’t return an object.

98
Q

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

A

Prototype property

99
Q

What does the instanceof operator do?

A

It tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. Returns a Boolean.

100
Q

What is a “callback” function?

A

It is a function passed in through another function as an argument.

101
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

Use setTimeout() function.

102
Q

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

A

Use setInterval() function.

103
Q

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

A

0 (zero)

104
Q

What do setTimeout() and setInterval() return?

A

setTimeout( ): the returned timoutID is a positive integer value which identifies the timer created by the call to setTimout( ).
setInterval( ):The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval( )

105
Q

What is AJAX?

A

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

106
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML (XML is precursor to JSON)

107
Q

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

A

XMLHttpRequest

108
Q

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

A

‘Load’ event.

109
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

They share a prototype. Both are chained to the EvenTarget Prototype.