Javascript Flashcards

1
Q

What is the purpose of variables?

A

To store data

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

How do you declare a variable?

A

Keyword var/let/const + name of variable

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

Using the assignment operator. (=)

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, $ dollar sign, _ underscore

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

A variable starting with a lowercase and a variable starting with an uppercase will create two separate variables.

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

Hold data type consisting of letters/ other characters. Any type of text content.

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

Hold any data type handling numbers/numeric values. Counting, calculating sums, determining size, moving the position, setting time, etc.

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

To hold data types of 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

Assign a value to the variable, also used to update the value given to a variable.

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
  • Using the assignment operator.
  • Do not need to use ‘var’ again, just the name and (=)
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 represents a placeholder value, might hold value in the future.
  • Undefined is a value that has not been thought of yet, does not hold value yet.
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

To help debug. It makes it clearer which variables are being logged and in which order.

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, undefined, null

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

A numerical value

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

What is string concatenation?

A

The joining of two or more strings using (+) to create a new string

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

What is string concatenation?

A

The joining of two or more strings using (+) to create a new string

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

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

A
  • This will combine the value of the string before and after the (+) operator (a concatenated string)
  • Addition for numerical numbers.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

True/False

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

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

A

This will add the number value on the right and assigns the result to the variable

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

What are objects used for?

A

To group together a set of variables and functions to create a model.

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

What are object properties?

A

Variables that are part of an object.

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

Describe object literal notation.

A

The object is in curly braces with its contents, then stored in a variable. Each key is separated from its value using a colon and each property and method is separated with a comma.

Var (varName) = { property: ‘string’, };

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

How do you remove a property from an object?

A
  • Use keyword delete then dot notation to identify the property or method you want to remove from the object.
  • Delete var.varName;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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

A

Using square brackets or dot notation.
Bracket:

varName[‘newString’] = ‘updatedVal’;

Dot: varName.newString = updatedVal;

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

What are arrays used for?

A

To create ordered list of data.

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

Describe array literal notation.

A

Var varName = [ ‘one’, ‘two’, ‘three’];
- Open brackets
- List of data values followed by commas

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

How are arrays different from “plain” objects?

A
  • Arrays are a special type of objects, they hold a related set of values
  • Arrays use numbers as indexes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
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
29
Q

What is the length property of an array?

A

Gives you the total number of items in an array

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

How do you calculate the last index of an array?

A
  • Subtracting 1 from the array length
  • arrayName.length - 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is a function in JavaScript?

A
  • An object that can be called to make code more dynamic.
  • A set of statements to perform a task or calculate a value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe the parts of a function definition.

A
  • Function keyword to begin the creation of a new function
  • An optional name for the function
  • Zero or more parameters surrounded by parentheses.
  • { } opening and closing curly brace for the start and end of the function code block.
  • Optional return statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Describe the parts of a function call.

A

Function name, 0 or more arguments surrounded by parentheses.

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

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

A
  • The definition has a series of statements to be run in the codeblock.
  • The function call is just the function name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is the difference between a parameter and an argument?

A
  • Parameter: a placeholder / stores local variables to the function
  • Argument: the value that we want to use for the function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Why are function parameters useful?

A

Parameters are useful because they can be replaced with different arguments to run the same code block on that argument.

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

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

A
  • Produces a value from the function.
  • Prevents any more code in the function’s code block from being run.
  • A return will exit the function, any code after a return will not be logged..
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Why do we log things to the console?

A

To check whether we get the desired results

39
Q

What is a method?

A
  • A function that is a property of an object that
  • contains a function definition. Actions that can be performed on objects.
40
Q

How is a method different from any other function?

A

Methods are associated with an object property whereas any other function themselves are objects

41
Q

How do you remove the last element from an array?

A

pop()

42
Q

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

A
  • Math.floor() method - takes any float values 1.9 → to 1
  • Math.round (not preferred) - based on the rounding system (less than .4 rounds down, greater than .5 rounds up)
  • Not relevant to question ceil() rounds up: 1.1 → 2
43
Q

How do you generate a random number?

A
  • Math.random() method
  • Returns 0-1
44
Q

How do you delete an element from an array?

A
  • shift()- beginning
  • pop() - end
  • splice() method
    Syntax: you can specify which index in the first argument and how many objects in the second argument
45
Q

How do you append an element to an array?

A
  • unshift() method
  • push() method
46
Q

How do you break a string up into an array?

A

split() method of the string

47
Q

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

A
  • No, the original string would still be present. You can console.log to make sure.
  • Strings cannot be changed in Java.
48
Q

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

A

50

49
Q

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

A

Not always

50
Q

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

A

40

51
Q

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

A

MDN

52
Q

Give 6 examples of comparison operators.

A

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

53
Q

What data type do comparison expressions evaluate to?

A

Boolean true or false

54
Q

What is the purpose of an if statement?

A
  • If statements set conditions that will run a code block whether it is true or false
  • If the initial condition is false, it will keep running until the last condition
55
Q

Is else required in order to use an if statement?

A

No it is not required, it is only needed if there a condition you want to run if the first is not true.

56
Q

Describe the syntax (structure) of an if statement.

A

If (keyword) if
Condition surrounded by parenthesis ex. (score > 50)
Opening curly brace {
Code to execute if value is true ex: congratulate()
Closing curly brace }

57
Q

What are the three logical operators?

A

And logical &&: both conditions must be true to pass

Or logical ||: one or the other condition must be true to pass

Not logical ! (also referred to as ‘bang’): inverts the boolean value

58
Q

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

A

Using the logical && or logical || operators
- Ex: ((5>2) && (2<=3)) will run both expressions and only give true if both are.
- Ex ((5>2) || (2>3)) This will run true because the first statement is true.
Using an else if

59
Q

What is the purpose of a loop?

A

The purpose of a loop is to perform repeated tasks based on a condition

60
Q

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

A

The condition expression in a loop dictates when a loop should stop running.

61
Q

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

A

Each pass through the loop.

62
Q

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

A

Before each iteration.

63
Q

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

A

Once before the loop begins.

64
Q

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

A

Before each loop iteration

65
Q

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

A

At the end of each loop iteration, until condition is false.

66
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

67
Q

What does the ++ increment operator do?

A

Increments the value by 1.

68
Q

How do you iterate through the keys of an object?

A

Using a for in loop.

69
Q

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

A

Focus

70
Q

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

A

Blur

71
Q

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

A

Input

72
Q

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

A

Submit

73
Q

What does the event.preventDefault() method do?

A
  • Prevents the browser from performing the default action of the event. In this exercise, it prevents the browser from automatically reloading the page with the form’s values in the URL
74
Q

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

A

Loads the form’s values in the URL

75
Q

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

A
  • Elements property
  • DOM.elements()
76
Q

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

A

Value property

77
Q

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

A

Being unable to find the point of where it is wrong later

78
Q

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

A
  • Being able to see any changes you made, being able to see console log to see if you got the correct data.
  • You can see any console error every time you save changes
79
Q

What is the event.target?

A

Element that triggered the event

80
Q

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

A

Hides the element

81
Q

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

A

CSS Selectors, returns true or false.

82
Q

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

A

getAttribute()

83
Q

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

A

Each step

84
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

Add an event listener to each tab

85
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

Else if statements

86
Q

What is JSON?

A

JSON is a string that represents structured data. It is used to easily transmit data.

87
Q

What are serialization and deserialization?

A
  • Serialization is turning data into a linear “string” of bytes to store information
  • Deserializaiton turning a string of bytes into an object in memory.
88
Q

Why are serialization and deserialization useful?

A

It easily helps transmit and store data.

89
Q

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

A

JSON.stringify

90
Q

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

A

JSON.parse

91
Q

How do you store data in localStorage?

A

You stringify the data and then use the localStorage.setItem() method.

92
Q

How do you retrieve data from localStorage?

A

localStorage.getItem() method

93
Q

What data type can localStorage save in the browser?

A

JSON strings

94
Q

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

A
  • Before the document is about to be unloaded (Document still visible and event can still be canceled)
  • Can ask the user to confirm before closing out/leaving a web page