JavaScript Quiz Flashcards

(70 cards)

1
Q

What is a variable in programming?

A

A variable is a bucket we can store data in and a way we can tell our program to remember values for us to use later on.

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

What are the two main things variables allow us to do?

A

Variables allow us to reuse a value and change a value throughout our program.

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

In variable declaration, what is let age = 25 doing?

A

let is the declaration (creating a space in memory), age is the assignment (putting stuff in the bucket), and 25 is the argument (the actual value being stored).

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

What types of data can you store in variables?

A

You can store strings (text surrounded by quotes), numbers (integers and floats like 4.16), and booleans (true/false values).

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

What is camelCase naming convention?

A

camelCase is a naming convention where the first word is lowercase and subsequent words start with uppercase, like numberOfApples or currentTime.

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

What are the three primitive data types?

A

Numbers (for calculations and counting), Strings (any sequence of characters including letters, spaces, numbers, or symbols), and Boolean values (representing true/false logical ideas).

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

What can you do with the String data type?

A

Strings can display data that uses text or symbols, add/remove text, and modify characters.

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

What is the purpose of Boolean values in programming?

A

Booleans represent logical ideas of true/false. We use them to determine validity and to make decisions in our programs.

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

What does the Number data type allow us to do?

A

Numbers allow us to describe, calculate, and count values in our programs.

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

What are operators in programming?

A

Operators are different symbols that represent an operation, such as +, -, *, /.

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

What are the three main categories of operators?

A

Arithmetic operators (for making calculations), comparison operators (for comparing information), and logical operators (for creating logical expressions).

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

What are the three logical operators?

A

AND, OR, and NOT operators, which are used to create logical expressions using boolean values.

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

What is the purpose of conditional statements?

A

Conditionals enable us to make decisions and do something if a statement is true. They are either true or false.

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

What is the basic syntax of an if statement?

A

if (condition is true) { // Do cool stuff } else if (condition is true) { // Do this other stuff } else { // Other stuff }

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

What are comparison operators and what do they return?

A

Comparison operators (==, ===, !=, !==, >, <, >=, <=) compare values and always return either TRUE or FALSE.

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

What’s the difference between == and ===?

A

ERROR!

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

How do you use the AND logical operator (&&)?

A

AND requires both conditions to be true. Example: if (name === “Leon” && status === “Baller”) { // Do something }

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

How do you use the OR logical operator (||)?

A

OR requires at least one condition to be true. Example: if (day === “Saturday” || day === “Sunday”) { // It’s the weekend }

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

What is an important rule about assignment (=) vs comparison (==, ===)?

A

Single equal sign (=) will always be reassignment, while 2-3 equal signs (==, ===) will always be comparison.

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

What is a function?

A

A function is a simple set of instructions or a named sequence of instructions, packaged as a unit, that performs a specific task.

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

Why are functions important in programming?

A

Functions form the basic “building blocks” of a program, make code reusable, and allow us to put a set of instructions into memory so we can use it over and over again.

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

What are the two parts of using a function?

A

Function declaration (when we define a function and specify the instructions, inputs, and name) and function call (when we tell the function to run and execute all its instructions).

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

What is the difference between parameters and arguments?

A

Parameters are the inputs specified when we define a function. Arguments are the actual values we give for each input when we call the function.

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

When does a function actually run?

A

A function won’t run unless it’s called. You write it once but can reuse it over and over again by passing in different arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the example `function yell(word) { alert(word) }` demonstrate?
It shows that `yell` is the function name, `word` is the parameter, and when called like `yell("HELLO")`, the argument "HELLO" falls into the parameter.
26
What is control flow?
Control flow is the order in which each program executes its set of instructions. This order is just as important as the instructions themselves.
27
What are the three types of control structures?
Conditionals (do instructions if something is true), Loops (do instructions a certain number of times), and Exceptions (do instruction set A, but if an error occurs, switch to instruction set B).
28
How do control structures allow for flexibility?
Control structures such as conditionals allow for control flow to be altered during the execution of a program.
29
What example is given for exception handling?
Collect shipping and email address; reject a purchase if either is incorrect.
30
What is the purpose of loops?
Loops enable us to repeat an action some number of times. They are structures in programming where instructions are written once but a computer can execute them multiple times.
31
What are the three types of loops covered?
For loops (count controlled), While loops (condition controlled), and For Each loops (collection controlled).
32
When should you use a for loop?
Use a for loop when you know the number of times you'd like to perform a task before you begin. Each execution is called an "iteration."
33
What is the syntax of a for loop?
for (let i = 1; i < 5; i++) { console.log(i) } where the first part is the initial expression (where the loop starts), second is the condition (when it stops), and third is the increment (what happens each time).
34
When should you use a while loop?
Use a while loop when you know when a program should stop, but not the number of times it should repeat. It's a condition-controlled loop.
35
How does a while loop work?
A while loop repeats a set of instructions while the condition is true. Example: let count = 0; while (count < 5) { console.log(count); count++; }
36
When should you use a for each loop?
Use a for each loop when you need to perform a task for every item in a list, or when the order of things must be maintained.
37
What does a for each loop do?
It defines a sequence and tells the computer to repeat the instructions for each item in the sequence.
38
What is a list (array) in programming?
Lists are one very basic data structure. Programmers use lists as a container to store multiple pieces of information that relate to each other in some way.
39
How is data ordered in an array?
Lists order our data in a specific, linear sequence. The position of a value in a list is known as its index.
40
What number do list indices start at?
List indices are numbers starting with 0. For example: cities = ['NY', 'LA', 'SYD', 'LDN'] has indices 0, 1, 2, 3.
41
How do you add items to the end of a list?
Using the .push() method. Example: myList.push('orange') adds 'orange' to the end.
42
How do you remove the last item from a list?
Using the .pop() method. Example: myList.pop() removes the last item.
43
How do you insert or remove items from the middle of a list?
Using the .splice() method. Example: myList.splice(1,1) removes items from the middle of the list.
44
How do you access an element from an array?
Using the index number. Example: newArr[0] returns 'Zebra', newArr[1] returns undefined if nothing is there.
45
Can you overwrite array values?
Yes, you can overwrite entire arrays by assigning an array to a different array. Example: cars = nums where cars = ['Honda', 'Toyota', 'Ford', 'Tesla'] and nums = [1,2,3].
46
What does array iteration allow you to do?
It iterates through an array, passing in the value and index for each element, allowing you to perform operations on each item.
47
What is the syntax for iterating through an array with forEach?
bestColors.forEach((x, i) => console.log(x)) where x is the element/value and i is the index. The arrow function will run each time.
48
How many times will a forEach loop run?
It's going to run 4 times if there are 4 elements in the array. It's a special function built into JS that will run once for each element in your array.
49
What does the .shift() method do?
It removes an item from the front of an array. Example: let removed = bestRappers2020.shift() removes the value in front of the array.
50
What does the .pop() method do?
It removes an item from the end of an array. Example: let removedAgain = bestRappers2020.pop()
51
What does the .unshift() method do?
It adds an item to the beginning of an array. Example: bestRappers2020.unshift('y') adds 'y' to the front.
52
What does the .push() method do?
It adds an item to the end of an array.
53
What does the .map() method do?
It loops through an array but it creates a new array rather than modifying the original.
54
What is an object in JavaScript?
In JS, everything is an object. Objects are a collection of variables and functions that represent the attributes and behavior of something used in a program.
55
What are object properties?
Object properties are variables attached to an object. They might contain a variable to represent hours, minutes, seconds.
56
What are object methods?
Object methods are functions attached to an object. They might contain functions that manipulate those values, such as start() and stop().
57
How do objects store data?
Objects store "keyed" collections of data as key-value pairs.
58
What is an example of an object representing a stopwatch?
A stopwatch object might have properties (attributes) like hours, minutes, seconds, and methods (behaviors) like start() and stop() functions.
59
What is literal notation for objects?
Literal notation uses the format: let stopwatch = {} where properties use dot notation like stopwatch.currentTime = 12 and method calls use stopwatch.tellTime(stopwatch.currentTime).
60
What is dot notation?
Dot notation is used to access object properties and methods. For properties: object.property, for methods: object.method().
61
When functions are tied to objects, what are they called?
When functions are tied to objects, they are called methods.
62
When individual variables are tied to objects, what are they called?
When individual variables are tied to objects, they are called properties.
63
What is the DOM?
The DOM (Document Object Model) is what happens every time you refresh the page. You're "repainting" the picture, re-rendering and re-reading the HTML, JS, and CSS to paint what you see in the browser.
64
What is an event listener?
An event listener (click) is when they hear the click, they tell a certain set of instructions to run. Example: document.getElementById('purple').onclick = partyPurple
65
What does document.querySelector('body') do?
It selects the body element from the document. You can then modify its style properties like document.querySelector('body').style.backgroundColor = 'rgba...'
66
How can you change text color using DOM manipulation?
Using: document.querySelector('body').style.color = 'white'
67
What is a variable in JavaScript?
A variable is an abstract storage location paired with a symbolic name that holds data. It's like a box where we store information.
68
What are the three ways to declare a variable in JavaScript?
let, const, and var. 'let' allows reassignment, 'const' creates constants that cannot be reassigned, and 'var' is the older method (function-scoped).
69
What are the naming rules for JavaScript variables?
Must start with a letter, $, or _; cannot contain spaces; avoid reserved keywords; use meaningful names.
70