Javascript Flashcards

1
Q

What is a console?

A

The console is a panel that displays important messages, like errors, for developers. If we want to see things appear on our screen, we can print, or log, to our console directly.
In JavaScript, the console keyword refers to an object, a collection of data and actions, that we can use in our code.

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

console.log()

A

When we write console.log() what we put inside the parentheses will get printed, or logged, to the console. This is important so we can see the work that we’re doing.
The console.log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

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

What are the two types of code comments in Javascript?

A

A single line comment and a multi-line comment

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

How do you write a single line comment?

A

// + comment
Note: You can also write a single line comment after a line of code.

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

How do you write a multi-line comment?

A

/* to begin the comment, and / to end the comment.
Note: you can also use this syntax to comment something out in the middle of a line of code.
Ex: console.log(/
IGNORED!*/ 5); // Still just prints 5

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

When is it a good time to use multi-line comments?

A

When preventing a block of code from running.

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

What are data types? How many fundamental data types are there?

A

Data types are the classifications we give to the different kinds of data that we use in programming. There are 7 fundamental data types.

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

What are the seven different fundamental data types?

A

Number, string, boolean, null, undefined, symbol, and object.

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

What is a string data type?

A

Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes: ‘ … ‘ or double quotes “ … “

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

What is a boolean data type?

A

A data type that only has two possible values- true or false. You can think of it as answers to a “yes” or “no” question.

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

What is a null data type?

A

This data type represents the intentional absence of a value, and is represented by the keyword null

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

What is an undefined data type?

A

Uses the keyword undefined (without quotes) and means that a given value does not exist. It represents the absence of a value like null but is used differently.

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

What is an object data type?

A

Collections of related data.

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

What are literals in JS?

A

Literals represent values in JavaScript. These are fixed values—not variables—that you literally provide in your script.

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

What are identifiers in JS?

A

Identifiers are JavaScript names. They are used to name variables and keywords, and functions.

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

What are the rules for identifiers in JS?

A

The rules for legal names are the same in most programming languages. A JavaScript name must begin with:

A letter (A-Z or a-z)
A dollar sign ($)
Or an underscore (_)
Subsequent characters may be letters, digits, underscores, or dollar signs.

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

What are expressions?

A

An expression is a single unit of JavaScript code that the JavaScript engine can evaluate, and return a value.

An expression can be a literal, like a string or a number. It can also be an identifier that points to a variable.

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

Name three types of simple expressions in JS.

A

Arithmetic expressions,
String expressions,
Logical expressions.

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

Explain arithmetic expressions

A

Arithmetic expressions are expressions that take a variable and an operator (more on operators soon), and result in a number:

1 / 2
i++
i -= 2
i * 2

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

Explain logical expressions

A

Logical expressions make use of logical operators and resolve to a boolean value:

a && b
a || b
!a

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

Explain string expressions

A

String expressions are expressions that result in a string:

‘A ‘ + ‘string’

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

What is an operator?

A

An operator is a character that performs a task in our code.

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

What is the keyboard symbol for the multiply operator?

A
  • Ex: 7 * 3 = 21
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the keyboard symbol for the division operator?

A

/
Ex: 9/3 = 3

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

What is the keyboard symbol for the remainder operator?

A

%
console.log(11 % 3); // Prints 2

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

What is the remainder operator sometimes called?

A

modulo

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

Name three of the operator precedence rules in JS

A

The precedence rules are:

  • / % (multiplication/division/remainder)
    + - (addition/subtraction)
    = (assignment)

Operations on the same level (like + and -) are executed in the order they are found, from left to right.

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

What is string concatenation?

A

Combining one string to another string use the + operator.
Ex: console.log(‘hi’ + ‘ya’); // Prints ‘hiya’

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

How would we include space in between two strings when we are using string concatenation?

A

We have to include empty parenthesis in between them or space at the end or beginning of a string.
Ex:
console.log(‘front ‘ + ‘space’);
// Prints ‘front space’
console.log(‘back’ + ‘ space’);
// Prints ‘back space’
console.log(‘no’ + ‘space’);
// Prints ‘nospace’
console.log(‘middle’ + ‘ ‘ + ‘space’);
// Prints ‘middle space’

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

What are properties?

A

Properties are the values associated with a JavaScript object.

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

What is the symbol used for the dot operator?

A

.
Ex: console.log(‘Hello’.length); // Prints 5

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

The JavaScript spread operator

A

The JavaScript spread operator (…) expands an iterable (like an array) into more elements. This allows us:

  1. to quickly copy all or parts of an existing array into another array* (when we want to create a new array from an existing array),

const fruits = [‘banana’, ‘pear’, ‘apple’]

const morefruits = […fruits]

console.log(morefruits)
//[ ‘banana’, ‘pear’, ‘apple’ ]
*only works with primitive data types

  1. to extract only what’s needed from an array,

const numbers = [1, 2, 3, 4, 5, 6];

const [one, two, …rest] = numbers;

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

What are methods?

A

Methods are actions we can perform. Data types have specific actions we can perform that allow us to handle instances of that data type.

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

How do we use or “call” methods?

A

We call, or use, methods by attaching an instance with:

a period (the dot operator)
the name of the method
opening and closing parentheses

Ex:’example string’.methodName().
Ex: console.log(‘hello’.toUpperCase()); // Prints ‘HELLO’

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

.random()

A

This method returns a random number between 0 (inclusive) and 1 (exclusive). Math.random() always returns a number lower than 1.To generate a random number between 0 and 50, we could multiply this result by 51, like so:
Math.random() * 51;

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

Math.floor()

A

This method takes a decimal number, and rounds down to the nearest whole number.

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

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

Math.ceil()

A

The Math.ceil() function always rounds up to the nearest integer.

Ex:
console.log(Math.ceil(.95));
// expected output: 1

Ex:
console.log(Math.ceil(4));
// expected output: 4

console.log(Math.ceil(7.004));
// expected output: 8

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

Number.isInteger()

A

method determines whether the passed value is an integer.

Ex:
console.log(Number.isInteger(2017))
//Prints true

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

How can we access properties and methods?

A

We can access properties and methods by using the ., dot operator.

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

What is a variable?

A

A variable is a container for a value. They are like little containers for information that live in a computer’s memory. You can have container, label it, and put “stuff” in it.

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

What three things can we do with variables?

A
  1. Create a variable with a descriptive name.
  2. Store or update information stored in a variable.
  3. Reference or “get” information stored in a variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

var

A

var, short for variable, is a JavaScript keyword that creates, or declares, a new variable.

Ex:
var myName = ‘Arya’;
console.log(myName);
// Output: Arya

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

What is myName when creating a variable?

A

This would be the variable name (labeling the container). It is standard convention to use camel casing.

Ex:
var myName = ‘Arya’;
console.log(myName);
// Output: Arya

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

What is camel casing?

A

In camel casing you group words into one, the first word is lowercase, then every word that follows will have its first letter uppercased.
Ex:
camelCaseEverything

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

What is the = sign when creating a variable?

A

= is the assignment operator. It assigns the value (‘Arya’) to the variable (myName).

Ex:
var myName = ‘Arya’;
console.log(myName);
// Output: Arya

46
Q

What are some important rules when naming variables?

A
  1. Variable names cannot start with numbers.
  2. Variable names are case sensitive, so myName and myname would be different variables. It is bad practice to create two variables that have the same name using different cases.
  3. Variable names cannot be the same as keywords.
47
Q

let

A

The let keyword signals that the variable can be reassigned a different value.

Ex:
let meal = ‘Enchiladas’;
console.log(meal); // Output: Enchiladas
meal = ‘Burrito’;
console.log(meal); // Output: Burrito

48
Q

What happens when we don’t assign a value to variable (var or let)?

A

If we don’t assign a value to a variable declared using the let keyword, it automatically has a value of undefined.
We can reassign the value of the variable.

Ex:
let price;
console.log(price); // Output: undefined
price = 350;
console.log(price); // Output: 350

49
Q

const

A

The const keyword is short for the word constant. Just like with var and let you can store any value in a const variable. A const variable cannot be reassigned.

Ex:
const myName = ‘Gilberto’;
console.log(myName); // Output: Gilberto

50
Q

How are const different from let and var?

A

a const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you’ll get a TypeError. Constants must also be assigned a value when declared. If you try to declare a const variable without a value, you’ll get a SyntaxError.

51
Q

How can we use variables and math operators to calculate new values and assign them to a variable?

A
  1. We can create a variable and assign a number value to it (let w = 4) and then we can reassign it a new value by using the math operators to perform a calculation (w = w + 1).
  2. We can also use the built-in mathematical assignment operators.

Ex:
let w = 4;
w += 1;

console.log(w); // Output: 5

52
Q

Explain the following line of code:
let w = 4
w +=1;

console.log(w); //Output: 5

A
  1. We create a variable and assign the value 4.
  2. We perform the mathematical operation of the first operator + using the number to the right (w + 1)
  3. Then we reassign w to the computed value.
53
Q

++

A

The increment operator will increase the value of the variable by 1.

Ex:
let a = 10;
a++;
console.log(a); // Output: 11

54
Q

A

The decrement operator will decrease the value of the variable by 1.

55
Q

How can we combine two string values even if those values are being stored in variables?

A

By using the + operator

Ex:
let myPet = ‘armadillo’;
console.log(‘I own a pet ‘ + myPet + ‘.’);
// Output: ‘I own a pet armadillo.’

56
Q

What is string interpolation?

A

String interpolation is when we insert, or interpolate, variables into strings using template literals.

Ex:
const myPet = ‘armadillo’;
console.log(I own a pet ${myPet}.);
// Output: I own a pet armadillo.

57
Q

What are template literals?

A

Template literals are string literals that allow a more powerful way to define strings, using the backtick:

const test = something

58
Q

Name two uses of template literals

A
  1. Using template literals you can perform string substitution with ${}, embedding the result of any JS expression:
    const variable = 1

const test = a string with the number ${variable}

const test2 = a string with the number ${variable + 10}

  1. They let you create multiline strings:

const test = a string defined on multiple lines

59
Q

What is a temperate literal wrapped in?

A

Backticks ` (this key is usually located on the top of your keyboard, left of the 1 key).

Ex:
const myPet = ‘armadillo’;
console.log(I own a pet ${myPet}.);
// Output: I own a pet armadillo.

60
Q

${…}

A

template literal syntax that allows you to interpolate variables and expressions into strings.

Ex:
const myPet = ‘armadillo’;
console.log(I own a pet ${myPet}.);
// Output: I own a pet armadillo.

61
Q

How can we check the data type of a variable’s value?

A

We can check the data type of a variable’s value by using the typeof operator.

Ex:
const unknown1 = ‘foo’;
console.log(typeof unknown1); // Output: string

const unknown2 = 10;
console.log(typeof unknown2); // Output: number

62
Q

W/hat are conditional statements?

A

A conditional statement checks a specific condition(s) and performs a task based on the condition(s).

63
Q

if() {}

A

Syntax for if statement
The if keyword followed by a set of parentheses () which is followed by a code block, or block statement, indicated by a set of curly braces {}.

Inside the parentheses (), a condition is provided that evaluates to true or false.

If the condition evaluates to true, the code inside the curly braces {} runs, or executes.

if (true) {
console.log(‘This message will print!’);
}
// Prints: This message will print!

64
Q

What are else statements?

A

Else statements run a block of code when the if statement evaluates to false.

An else statement must be paired with an if statement, and together they are referred to as an if…else statement.

65
Q

What are comparison operators?

A

Comparison operators compare the value on the left with the value on the right.

Ex:
10 < 12 // Evaluates to true

66
Q

What are some handy comparison operators?

A

Less than: <
Greater than: >
Less than or equal to: <=
Greater than or equal to: >=
Is equal to: ===
Is not equal to: !==

67
Q

What is the syntax for the identity operator?

A

===

Ex:
‘apples’ === ‘oranges’ // false

68
Q

What are logical operators?

A

Operators that work with boolean values.There are three logical operators:
the and operator (&&)
the or operator (||)
the not operator, otherwise known as the bang operator (!)

69
Q

&&

A

When we use the and (&&) operator, we are checking that two things are true. Both conditions must evaluate to true for the entire condition to evaluate to true and execute. Otherwise, if either condition is false, the && condition will evaluate to false and the else block will execute.

Ex:
if (stopLight === ‘green’ && pedestrians === 0) {
console.log(‘Go!’);
} else {
console.log(‘Stop’);
}

70
Q

||

A

We use the or operator (II) when we only care about either condition being true. If the first condition in an || statement evaluates to true, the second condition won’t even be checked.

Ex:
if (day === ‘Saturday’ || day === ‘Sunday’) {
console.log(‘Enjoy the weekend!’);
} else {
console.log(‘Do some work.’);
}

71
Q

!

A

The not operator or bang operator (!) Reverses, or negates, the value of a boolean. The ! operator will either take a true value and pass back false, or it will take a false value and pass back true.

72
Q

Why do we use logical operators?

A

We use logical operators to add another layer of logic to our code.

73
Q

What are truthy values?

A

A truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy.

74
Q

What are falsy values?

A

Falsy value are values that are considered false when encountered in a Boolean context. The following are considered falsy values:
0
Empty strings like “” or ‘’
null which represent when there is no value at all
undefined which represent when a declared variable lacks a value
NaN, or Not a Number

75
Q

What is short-circuit evaluation?

A

Short-circuit evaluation is when we assign a truthy or falsy value to a variable using the or (II) operator.

Ex:
let username = ‘’;
let defaultName = username || ‘Stranger’;

console.log(defaultName); // Prints: Stranger

76
Q

What are ternary operators?

A

Ternary operators are short- hand syntax for if-else statements. We use a ? followed by :

Ex:
let isNightTime = true;

if (isNightTime) {
console.log(‘Turn on the lights!’);
} else {
console.log(‘Turn off the lights!’);
}

Using ternary operator:
isNightTime ? console.log(‘Turn on the lights!’) : console.log(‘Turn off the lights!’);

77
Q

What are else if statements?

A

Else if statements are added to if else statements and allow you to add more than two possible outcomes for a conditional statement.The else if statement always comes after the if statement and before the else statement.

Ex:
let stopLight = ‘yellow’;

if (stopLight === ‘red’) {
console.log(‘Stop!’);
} else if (stopLight === ‘yellow’) {
console.log(‘Slow down.’);
} else if (stopLight === ‘green’) {
console.log(‘Go!’);
} else {
console.log(‘Caution, unknown!’);
}

78
Q

What are switch statements?

A

Switch statements execute a block of code if the value entered into the switch keyword matches the case it’s being compared to. Switch statements can replace multiple else if statements. depending on different cases.

79
Q

What are cases clauses?

A

A case clause used to match against an expression. If the value and the expression match, then the block of code will be executed.

80
Q

What does the break keyword do?

A

The break keyword tells the computer to exit the block and not execute any more code.

81
Q

What is scope?

A

Scope is the context in which our variables are declared. We think about scope in relation to blocks because variables can exist either outside of or within these blocks.

82
Q

What are blocks?

A

A block is the code found inside a set of curly braces {}.

83
Q

Why are blocks important?

A

Blocks help us group one or more statements together and serve as an important structural marker for our code.

84
Q

What is global scope?

A

variables that are declared outside of blocks.

85
Q

What are global variables?

A

Global variables are variables that are declared outside of blocks. Because global variables are not bound inside a block, they can be accessed by any code in the program, including code in blocks.

86
Q

What is block scope?

A

Block scope is when a variable is defined inside a block, it is only accessible to the code within the curly braces {}. We say that variable has block scope because it is only accessible to the lines of code within that block.

87
Q

What are local variables?

A

Local variables are variables that are only available to the code that is part of the same block.

88
Q

What is global namespace?

A

When you declare global variables, they go to the global namespace. The global namespace allows the variables to be accessible from anywhere in the program.

89
Q

What is scope pollution and why is it bad?

A

Scope pollution is when we have too many global variables that exist in the global namespace or variable names are reused. Scope pollution makes it difficult to keep track of our different variables.

90
Q

Why should we scope our variables as tightly as possible using block code?

A
  1. It makes our code easier to read because they are in blocks or discrete sections.
  2. It clarifies which variables are associated with different parts of the program rather than having to keep track of them line after line!
  3. It is easier to maintain the code since it is broken up into smaller parts.
  4. It saves memory in our code because it no longer exist after the block finishes running.
91
Q

What is global namespace?

A

Global namespace is the space in our code that contains globally scoped information.

92
Q

What are arrays?

A

Arrays are JavaScript’s way of making lists. Arrays can store any data types (including strings, numbers, and booleans). Arrays are also ordered (each item has a numbered position)

93
Q

What are array literals?

A

Array literals create an array by wrapping items in square brackets [].

94
Q

What are the ways to create an array.

A
  1. Using array literals
95
Q

What is the content inside of an array called?

A

An element

96
Q

What is an index?

A

An index is a numbered position for an element. Each element in an array will have a numbered position.

97
Q

Arrays are zero-indexed. What does this mean?

A

Zero-indexed means that the positions start counting from 0 rather than 1. Therefore, the first item in an array will be at position 0.

98
Q

How can we update an element in an array?

A

We can update an element in an array by using the following syntax:

variableName[3] = ‘new value’;

99
Q

What is the difference between using let and const when working with arrays?

A

When we use the let variable when working with arrays we can reassign a new array and update an element in the array. With const variables we can update an element in an array but we can not reassign a new array.

100
Q

.length

A

The .length property is one of an array’s built in properties. It returns the number of items in the array.

Ex:
const newYearsResolutions = [‘Keep a journal’, ‘Take a falconry class’];

console.log(newYearsResolutions.length);
// Output: 2

101
Q

.push()

A

The .push() method allows you to add items to the end of an array. The .push() changes, or mutates, the array. This method is also called a destructive array method since it changes the initial array. When we add this method to an array we can call the array like we would a function.

Ex:
const itemTracker = [‘item 0’, ‘item 1’, ‘item 2’];

itemTracker.push(‘item 3’, ‘item 4’);

console.log(itemTracker);
// Output: [‘item 0’, ‘item 1’, ‘item 2’, ‘item 3’, ‘item 4’];

102
Q

.pop()

A

The .pop method removes the last item in an array. It does not take any arguments. This method also mutates the array. The .pop() method also returns the value of the last element. We can store this in a variable to use later in the code.

Ex:
const newItemTracker = [‘item 0’, ‘item 1’, ‘item 2’];

const removed = newItemTracker.pop();

console.log(newItemTracker);
// Output: [ ‘item 0’, ‘item 1’ ]
console.log(removed);
// Output: item 2

103
Q

.shift()

A

The .shift method removes and returns the first element of the array. All of the elements after it will shift down one place.

104
Q

.unshift()

A

Adds one or more elements to beginning of array and returns new length.

105
Q

.slice()

A

The .splice() method modifies an array by inserting, removing, and/or replacing array elements then returning an array of removed elements. The splice array is a shallow copy of the original array.

106
Q

.indexOf()

A

The .indexOf() method returns the first index at which an element can be found. Returns -1 if the element is not found.

107
Q

What happens if we try to change an array inside a function?

A

When you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. pass-by-reference)

108
Q

.join()

A

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

109
Q

Keywords

A

Keywords are words that are built into the JavaScript language, so the computer recognizes them and treats them specially.

110
Q

Node.js

A

Node.js is a JavaScript runtime, or an environment which runs JavaScript code outside of the browser.
Node was created with the goal of building web servers and web applications in JavaScript, but it’s a powerful and flexible environment that can be used for building all sorts of applications.