JavaScript Flashcards

1
Q
var fullName = 'Cody Miller';
var isCool = true;
var totalPets = 1000;
A

on line 1 the string ‘Cody Miller’ is being assigned to the new variable fullName;
on line 2 the boolean true is being assigned to the new variable isCool;
on line 3 the number 1000 is being assigned to the new variable totalPets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
var firstName = 'Ron';
var lastName = 'Swanson';
var fullName = firstName + ' ' + lastName;
A

on line 1, the string ‘Ron’ is being assign to the new variable firstName;
on line 2, the string ‘Swanson’ is being assign to the new variable lastName;
on line 3, the value of the variable firstName is being concatonated with the string ‘ ‘, the result of that expression is being concationeted with the value of the variable lastName, and the result of that expression is being assigned to tthe new variable fullName.

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

console. log(‘value of isAcidic:’, isAcidic);

console. log(‘typeof isAcidic:’, typeof isAcidic);

A

on line 1 we have the log method of the console object being called with two arguments: the string ‘value of isAcidic’ and the value of the isAcidic variable;
on line 2 we have the log method of the console object being called with two arguments: the string ‘typeof isAcidic’ and the result of the expression ‘typeof isAcidic’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
var pet = {
  name: 'Bilu',
  type: 'doggo'
};

delete pet.name;
delete pet.type;

A

on line 1, we have an object literal being assigned to the new variable pet;
on line 2, we have the string ‘Bilu’ being assigned to the property name;
on line 3, we have the string ‘doggo’ being assigned to the property type;
on line 4, we have the closing curly braces for the object literal;

on line 6, we have the delete operator being used on the name property of the pet object;
on line 7, we have the delete operator being used on the type property of the pet object.

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

What is the purpose of variables?

A

To store data for later use

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

How do you declare a variable?

A

with the keyword var, const or let, followd by the name of the new variable

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

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

A

keyword (let, const, var) variableName = value;

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

What characters are allowed in variable names?

A

upper/lower case letters, $, _, and numbers when not in the first position

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

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

A

that upper case letters and lower case letters are dealt as different characters, and so “cat” and “Cat” are dealt as different names.

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

What is the purpose of a string?

A

to store text data

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

What is the purpose of a number?

A

to be use in arithmetic operations, store numerical data for later use and manipulation, etc.

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

What is the purpose of a boolean?

A

to store true/false values, allowing for binary operations, branching decision making, etc.

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

What does the = operator mean in JavaScript?

A

the assignment of new value, from the right side to the left

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

How do you update the value of a variable?

A

variableName = newValue;

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

What is the difference between null and undefined?

A

Null is an empty value that does not appears naturally, and thus can only appear where intentionally put there by the programmers;

Undefined is an empty value that can appear as result of errors in the code, and thus is more useful if never assigned by the programmers;

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

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

A

So to identify promptly the source of any error that shows up in your console.

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

Give five examples of JavaScript primitives.

A

number, string, boolean, undefined, null

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

What data type is returned by an arithmetic operation?

A

number

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

What is string concatenation?

A

the creation of a nem string by adding two or more strings at the tail end of the one at the left side of a + sign

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

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

A

addition and concatenation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
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
22
Q

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

A

adds (or concatanates) the value of the variable to a new value, and then assigns the result of that operation to the original variable

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

What are objects used for?

A

storing variables (as properties) and functions (as methods) to create a relationship between them and the object, usually to better describe a real world structure

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

What are object properties?

A

variables stored inside objects.

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

Describe object literal notation.

A
var object = {
  property: value,
  method (arguments) = {

}
};

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

How do you remove a property from an object?

A

using the delete operator:

delete object.property;

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

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

A

object.property or object[‘property’]

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

What are arrays used for?

A

to store a list of values, accecible by an index. It’s when the order or the quantity is the important factor, not the name of properties, that arrays come in hand

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

Describe array literal notation.

A

var array = [ , , , ];

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

How are arrays different from “plain” objects?

A

they have indexed values instead of property:value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
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
32
Q

What is the length property of an array?

A

the number of items in an array

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

How do you calculate the last index of an array?

A

array.length -1

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

What is a function in JavaScript?

A

a reusable block of code that can accept input and return a result, or execute an action and return no value.

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

Describe the parts of a function definition.

A
  1. function: keyword to declare a function;
  2. name (optional) how to call the function in the future: name();
  3. ( , , , ): a comma-separated list of zero or more parameters, surrounded by () parentheses;
  4. the start of the function’s code block, as indicated by an { opening curly brace;
  5. lines of code;
  6. return (optional): a statement to return a value;
  7. the end of the function’s code block, as indicated by a } closing curly brace.
function name( , , , ) {
  //lines of code
  //return
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Describe the parts of a function call.

A
  1. name(): the name of the function being called;
  2. ( , , , ): a comma-separated list of zero or more arguments, surrounded by () parentheses;

name( , , , );

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

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

A

we have 1. function (keyword), 2. parameters (placeholder names) instead of arguments (values) and 3. code block between curly braces on function definitions in contrat with function calls.

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

What is the difference between a parameter and an argument?

A

a parameter is a placeholder in the function definition for the values to be passed as arguments in a function call.

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

Why are function parameters useful?

A

without them, functions can’t accept input (in the form of arguments) later on, so they’ll aways execute the same actions without variation.

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

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

A

it 1. exists the function and 2. returns a value from the function (to be stored, used, manipulated, etc.)

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

Why do we log things to the console?

A

to see and debug the code as we write it.

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

What is a method?

A

a function passed as a property of an object

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

How is a method different from any other function?

A

They’re not, though methods are related to their objects while functions are not

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

How do you remove the last element from an array?

A

array.pop()

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

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

A

Math.floor();

46
Q

How do you generate a random number?

A

Math.random();

47
Q

How do you delete an element from an array?

A

array.splice();

48
Q

How do you append an element to an array?

A

array.push();

49
Q

How do you break a string up into an array?

A

string.split();

50
Q

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

A

No; and by logging the original string afterwards.

51
Q

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

A

As many as thirty, and some more;

52
Q

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

A

No.

53
Q

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

A

as many as thirty, and some more

54
Q

What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

MDN

55
Q

Give 6 examples of comparison operators.

A
< ,
 > ,
<=,
 >=,
 ===,
 !==
56
Q

What data type do comparison expressions evaluate to

A

boolean

57
Q

What is the purpose of an if statement?

A

check if a condition is true before running (or skipping) the code block

58
Q

Is else required in order to use an if statement?

A

no

59
Q

Describe the syntax (structure) of an if statement.

A

if (condition) {
block code;
}

60
Q

What are the three logical operators?

A

&&, || and !

61
Q

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

A

with the && or || logical operators

62
Q

What is the purpose of a loop?

A

to repeat a piece of code for a set number of iteration

63
Q

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

A

To check if the loop should repeat or if it should exit.

64
Q

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

A

Each single repetition of the loop’s code block

65
Q

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

A

before the start of each iteration.

66
Q

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

A

at the start, before the first condition evaluation

67
Q

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

A

before the start of each iteration

68
Q

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

A

after each iteration, just before the condition expression evaluation

69
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

70
Q

What does the ++ increment operator do?

A

takes the value of a variable, adds one to it (increments it by 1), and assign the result of that expression to the original variable

71
Q

How do you iterate through the keys of an object?

A

using the for ( in ) loop

72
Q

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

A

The ‘focus’ event

73
Q

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

A

The ‘blur’ event

74
Q

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

A

The ‘input’ event

75
Q

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

A

The ‘submit’ event

76
Q

What does the event.preventDefault() method do?

A

prevents the default behaviors to be triggered by the event

77
Q

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

A

reloads the page

78
Q

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

A

node.elements[ ];

79
Q

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

A

element.value;

80
Q

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

A

to get an error message and not known where the error is originating from

81
Q

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

A

to check in real time if the code is doing what you intend it to do

82
Q

What is the event.target?

A

the DOM object representing the HTML element where the event was triggered

83
Q

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

A

it vanishes from the viewport, removing itself from the document flow

84
Q

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

A

a string with the syntax of a CSS selector, and it returns a boolean (true if the element can be found with the selector, false otherwise);

85
Q

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

A

$element.getAttribute(‘attribute name’);

86
Q

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

A

at every step

87
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

using new functions

88
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

with switch statements/ if statements

89
Q

What is JSON?

A

Javascript Object Notation is a format made to serialize and store data;

90
Q

What are serialization and deserialization?

A

Serialization is the process of transforming complex data into a single string of bites;
Deserialization is the process to reverse serialization;

91
Q

Why are serialization and deserialization useful?

A

Serialization is useful for storage and transmission of data;
deserialization is useful for dealing with the content of the data;

92
Q

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

A

JSON.stringify();

93
Q

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

A

JSON.parse();

94
Q

How to you store data in localStorage?

A

passing the data to store as an argument of the JSON.stringfy() method, then passing the resulting DOMString as the value under a given key of an key:value pair in the localStorage using the localStorage.setItem(key, value) method

95
Q

How to you retrieve data from localStorage?

A

wit the method localStorage.getItem(key), you’ll retrieve the DOMString value at the key location of the localStorage. Then, just use the method JSON.parse() with said DOMString as an argument to get the original data stored;

96
Q

What data type can localStorage save in the browser?

A

it can save DOMString

97
Q

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

A

just before you leave/close the current page

98
Q

What is a method?

A

a(n usually anonymous) function passed as the value of a property in an object

99
Q

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

A

method definitions have the function keyword, the paramethers (placeholders for future arguments) inside the parentesis when present, and the function code block being assign to a property in an object.

method call have the object, a dot notation and the prperty name for the method, followed by the parentesis with any arguments.

100
Q

Describe method definition syntax (structure).

A
object = {
...,
  methodName: function (paramethers, , ) {
   // code block
},
...
};

or

object.methodName = function (paramethers, , ) {
   // code block
};
101
Q

Describe method call syntax (structure).

A

object.methodName(arguments, , );

102
Q

How is a method different from any other function?

A

it isn’t, or rather, it can’t be called free from it’s parent object

103
Q

What is the defining characteristic of Object-Oriented Programming?

A

both the data and behavior are saved in the same object, as its properties and methods

104
Q

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

A

Abstraction
Encapsulation
Inheritance
Polymorphism

105
Q

What is “abstraction”?

A

being able to work with (possibly) complex things in simple ways. Creating thus a simplified model that represents something. In computer science, you represent things as objects, describe their relevant properties, and the relevant behavior they can do or suffer is described in their methods;

106
Q

What does API stand for?

A

application programming interface (API)

107
Q

What is the purpose of an API?

A

to export, import and use complex code and data that you yourself hasn’t built from scratch. Is what allows one to build upon the work of others in the community

108
Q

What is this in JavaScript?

A

an implicit paramether present in every function that represents the object where said function is being called on;

109
Q

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

A

that it is a paramether even though you can’t see it and didn’t wrote it in the function definition

110
Q

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

A

call time

111
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

undefined