JS Basics/ES5 Flashcards

1
Q

How many data types are there in JavaScript?

Name them.

A

8:

undefined

null

boolean

string

symbol

bignit

number

object

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

How is a variable declared in JS? Give an example.

A

We declare a variable by putting the keyword var in front of it:

var myName;

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

How are statements ended in JS?

A

With a semicolon (;).

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

What are the rules for variables’ naming?

A

Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number.

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

What is the diffence between declaring and initialising a variable? What is the quickest way to do both?

A

Declaring is creating (naming) a variable. Initialising in assigning a value to the variable for the first time.

The quickest way to do both is to do them in one line:

var myName = “anti”;

or

var myNumber = 7

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

What is a string literal?

A

A series of zero or more characters enclosed in single or double quotes.

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

What is the default value of an declared but not initialised variable?

What message do you get when you try to use a mathematical operation involving such a variable?

What about string concatenation?

A

undefined

for mathematical: NaN (Not a Number)

for concatenation: undefined

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

Is JS case sensitive?

A

Yes.

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

What is best practice in naming variables?

Give an example.

A

Write variable names in JavaScript in camelCase. In camelCase, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.

Example:

var someVariable;

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

What is the difference between var and let?

A

When a variable is declared using the var keyword, it can be re-declared without throwing an error; but when the let keyword is used, each variable must have unique names i.e. trying to re-declare it with the same name will thow an error.

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

How is a read-only variable declared?

Give an example.

A

Use the const keyword.

Example:

const myReadOnlyVariable;

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

Compare the kewords const and let in JS.

A

const has all the features of let but it is read-only.

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

What is best practice in naming constants (variables that are not supposed to be changed)?

A

Should be all upper case.

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

What are two other names for decimal numbers in JS?

A

Floating point numbers or just floats

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

In JS, what is the remainde operation symbol? What does it do?

A

%

It returns the remainder of a division: e.g. 5%2 = 1

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

What is the most common usage of the remainder operation?

A

To find out if a number is even or odd. If the remainder is zero then the number is even.

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

Comment on remainder vs modulus.

A

They are not the same.

The remainder does not work well with negative numbers.

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

In programming, which side of an operation in evaluated first?

A

The right side of the assignment.

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

What do the += and -= operators do?

What about *= and /=?

A

They add or subtract what’s on the right-hand side to or from the variable on the left-hand side of the operator, respectively.

The others do the same but with their respective operations.

E.g. myVar *= 3; is the same as myVar = myVar * 3;

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

In JS, how can you place quotes inside quotes?

A

By using the escape () symbol before the inner quotes.

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

Comment on the use of single vs double quotes in JS.

A

They can both be used to create string literals as long as the string starts and ends with the same type of quote.

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

What are the 8 most common uses of the escape character?

A

' - single quote

" - double quote

\ - backslash

\n - new line

\t - tab

\r - carriage return

\b - word boundary

\f - form feed

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

In JS, when you use a + with Strings, what is it called?

A

Concatenation

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

Can we use += and *= in concatenation?

A

Yes.

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

In JS, how can we find out how long a strig is?

A

By appending .length to the string literal or to a variable where the string literal is stored.

E.g.

console.log(myVar.lenght);

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

What is Zero-based indexing?

A

It’s the property of programming languages that dictates that the code starts counting at 0 rather than 1.

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

What is the bracket notation?

Give an example.

A

It is a way to get a character at a specific index within a string.

E.g. If const firstName = “Charles”, you can get the value of the first letter of the string by using firstName[0].

const firstName = “Charles”;

const firstLetter = firstName[0];

console.log(firstLetter) would return C

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

In JS, what is string Immutability?

A

The property that dictates that a single character in a string literal cannot be changed.

The only way to change a string literal is to assign a new string to the variable stroring it.

let myStr = “Bob”;

myStr[0] = “J”;

The code above would return an error.

The code below is the only way to do it:

let myStr = “Bob”;

myStr = “Job”;

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

How can you get the last character of a string using bracket notation and the .length method?

A

const firstName = “Anti”;

const lastLetter = firstName[firstName.length - 1];

This would assign the letter “i” to lastLetter.

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

How can you use bracket notation to find the nth-to-last character in a string?

A

const firstName = “Augusta”;

const thirdToLastLetter = firstName[firstName.length - n];

If n=3, then thirdToLastLetter= “s”.

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

What is the below line of code:

const sandwich = [“peanut butter”, 55 , “bread”, 45.6, myVar];

A

The declaration of an array.

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

What is the code line below?

const teams = [[“Bulls”, 23], [“White Sox”, 45]];

A

A multidimetional array aka nested arrays i.e. arrays as elements of an array.

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

How is array data accessed?

A

Using indexing, just like with strings.

const array = [50, 60, 70];

array[0]; //50

const data = array[1]; //60

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

Are elements of an array mutable?

A

Yes, at will; even if the array is declared as a const.

const ourArray = [50, 40, 30];

ourArray[0] = 15; //50 is replaced by 15

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

Comment on the below:

array [0];

A

There shouldn’t be any spaces between the array name and the square brackets, like array [0]. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.

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

What would the below return?

const arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14] ];

arr[3][0][1];

What is it called?

A

11

Multidimetional indexing.

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

In JS, what does the .push() method do in relation to arrays?

Visualise examples.

A

It adds specified parameters at the end of an array.

const arr1 = [1, 2, 3];

arr1.push(“sam”); //[1, 2, 3, “sam”]

or

const arr2 = [“Stimpson”, “J”, “cat”];

arr2.push([“happy”, 6]); //[“Stimpson”, “J”, “cat”, “happy”, 6]

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

In JS, what does the .pop() method do in relation to arrays?

Visualise an example.

A

It removes the last parameter of an array.

const threeArr = [1, 4, 6];

const oneDown = threeArr.pop();

console. log(oneDown); //6
console. log(threeArr); //[1, 4]

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

In JS, what does the .shift() method do in relation to arrays?

Visualise an example.

A

It does the same as .pop() but removes the first element.

const ourArray = [“Stimpson”, “J”, [“cat”]];

const removedFromOurArray = ourArray.shift();

console. log(removedFromOurArray); //[“Stimpson”]
console. log(ourArray); //[“J”, [“cat”]]

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

In JS, what does the .unshift() method do in relation to arrays?

Visualise an example.

A

It does the same as .push() but adds parameters to the beggining of the array.

const ourArray = [“Stimpson”, “J”, “cat”];

ourArray.shift(); //[“J”, “cat”]

ourArray.unshift(“Happy”); //[“Happy”, “J”, “cat”]

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

In JS, what is a function?

Visualise an example of how to declare a function.

A

It is a way to reuse code by calling a defined function’s name.

A function is declared as below:

function functionName() {

console.log(“Hello World”);

};

Each time the funtionName() function is invoked, a message of Hello World will appear in the console.

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

In JS functions, what are parameters?

Visualise an example.

A

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called.

When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.

function testFun(param1, param2) {

console.log(param1, param2);

}

Then we can call testFun like this: testFun(“Hello”, “World”);. We have passed two string arguments, Hello and World. Inside the function, param1 will equal the string Hello and param2 will equal the string World. Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments.

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

In JS, what does the return statement do?

Visualise an example.

A

We can pass values into a function with arguments. Conversely, you can use this statement to send a value back out of a function.

function plusThree(num) {

return num + 3;

}

const answer = plusThree(5); // answer will be 8 becasuse plusThree(5) has a value of 8.

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

In JS, what is visibility?

A

It referes to what variables can be “seen” (manipulated and refered to) at given parts of the code.

45
Q

What kind of scope do variables declared outside of functions have?

A

Global.

46
Q

What does Global mean, in terms of scope?

A

It can be “seen” from anywhere in your code.

47
Q

What happens if you declare a variable without using let or const?

A

Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with let or const.

48
Q

In JS, what does local scope mean?

A

It means that a variable can only be “seen” inside the function where it is declared.

49
Q

What scope has precedence over the other?

A

Local variables always take precedence over global ones.

50
Q

Is it mandatory that a function has a return statement?

A

A function can include the return statement but it does not have to. In the case that the function doesn’t have a return statement, when you call it, the function processes the inner code but the returned value is undefined.

51
Q

In computer science, what is a queue?

A

An abstract Data Structure where items are kept in order. New items can be added at the back and old items are taken off from the front.

Think FIFO.

52
Q

What values can Boolean variables assume?

A

true or false

53
Q

When can a value be both true and false?

A

Never. These are mutually exclusive values.

54
Q

What is the difference between “true”/”false” and true/false.

A

The earlier are string values and the latter are boolean values, which completely different.

55
Q

Visualise the pseudocode for an IF statements logic.

A
56
Q

What is the difference between the equality and assigment operators?

A

Equality “==”: compares values and returns a boolean.

Assignment “=”: assigns the right-hand-side to the left-hand-side.

57
Q

What must JS do to values of different data types before comparing them? What is the technical name for it?

A

It must convert one of them to match the other.

Type Coersion

58
Q

What is the difference between the equality operator (==) and the strict equality operator (===)?

A

The strict equality operator does not perform Type Coersion i.e. only equal values of the same type will return true

59
Q

How can you determine the data type of a variable?

A

using the keyword typeof

60
Q

In JS, what is the inequality operator?

A

!=

61
Q

What is the strict inequlity operator in JS?

A

!==

62
Q

What is the greater-than operator in JS?

A

>

63
Q

In JS, what is the greater-than-or-equal-to sign?

A

>=

64
Q

What is the less-than operator in JS?

A

<

65
Q

In JS, what is the less-than-or-equal-to sign?

A

<=

66
Q

Visualise code that does the same as the code below but where the logic is written in one line.

A

Logical “OR” (||)

67
Q

Visualise code that does the same as the code below but where the logic is written in one line.

A

Logical “AND” (&&)

68
Q

Visualise an “else if” code block with one “else if” statement.

A
69
Q

Comment on the order of the “if, else if” statements.

A

The first true statement is the one that’s going to be returned, so care must be taken to ensure the logic is correct.

70
Q

What is a “switch” statement?

A

If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements which define various possible values. Statements are executed from the first matched case value until a break is encountered.

Case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

71
Q

In a “switch” code block, what is the “default” used for?

A

In a switch statement you may not be able to specify all possible values as case statements. Instead, you can add the default statement which will be executed if no matching case statements are found. Think of it like the final else statement in an if/else chain.

72
Q

How can you use multiple values with the same outcome in a “switch” block?

A

If the break statement is omitted from a switch statement’s case, the following case statement(s) are executed until a break is encountered. If you have multiple inputs with the same output, you can represent them in a switch statement like this:

73
Q

What’s the value added by the “switch” code block?

A

It is quicker to code that a long chain of “if, else if”.

74
Q

What is a better way to write the below code:

A
75
Q

When does a function conclude running and what happens after that?

A

It concludes running when it performs all that is in it. If it has a “return” statement, then it concludes when it reaches it. If there is anything after the “return” statement, then it will be ignored.

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

Below, “console.log(“byebye”)” will be ignored:

76
Q

In JS, what is an object?

A

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.

77
Q

In JS, what types of data can be used to represent object properties?

A

Strings and numbers.

Any other data type will be type-cast as a string.

78
Q

In JS, what is the notation used to access properties in an object?

A

There are two ways to access the properties of an object: dot notation (.) and bracket notation ([]), similar to an array.

Dot notation is what you use when you know the name of the property you’re trying to access ahead of time.

79
Q

Explain the bracket ([]) notation for objects in JS.

A

The second way to access the properties of an object is bracket notation ([]). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.

However, you can still use bracket notation on object properties without spaces.

Note that property names with spaces in them must be in quotes (single or double).

80
Q

Explain another bracket ([]) notation for objects in JS apart from strings.

A

Another use of bracket notation on objects is to access a property which is stored as the value of a variable. This can be very useful for iterating through an object’s properties or when accessing a lookup table.

81
Q

In JS, how are object properties updated?

A

After you’ve created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.

82
Q

In JS, how are object properties added?

A

You can add new properties to existing JavaScript objects the same way you would modify them.

83
Q

In JS, how are object properties deleted?

A

Use the delete keyword.

84
Q

How can objects be used for lookups?

A

Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to lookup values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

Here is an example of a simple reverse alphabet lookup:

85
Q

How can you check if an object has a given property?

A

We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

86
Q

What does JSON stand for?

A

JavaScript Object Notation.

Note: You will need to place a comma after every object in the array, unless it is the last object in the array.

87
Q

How can you access nested objects?

A

The sub-properties of objects can be accessed by chaining together the dot or bracket notation.

88
Q

How are nested arrays accessed?

A
89
Q

When accessing object properties that will be defined by the parameters of a funtion, what’s the best notation to use to manipulate said properties?

A

Better use the [] notation as the parameter/variables will be eitther strings or numbers.

90
Q

What is a “while” loop?

Add the numbers 5 through 0 (inclusive) in descending order to myArray using a while loop.

A

A loop that runs while a specified condition is true and stops once that condition is no longer true.

91
Q

What is a “for” loop?

How is it declared?

A

The most common type of JavaScript loop, which runs for a specific number of times.

For loops are declared with three optional expressions separated by semicolons:

for (a; b; c), where a is the initialization statement, b is the condition statement, and c is the final expression.

The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.

The condition statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to true. When the condition is false at the start of the iteration, the loop will stop executing. This means if the condition starts as false, your loop will never execute.

The final expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.

In the following example we initialize with i = 0 and iterate while our condition i < 5 is true. We’ll increment i by 1 in each loop iteration with i++ as our final expression.

92
Q

Which is the most common loop in Javascript?

A

“For” loop

93
Q

Visualise a “for” loop that adds only the even numbers from 0 to 10 to an array.

A
94
Q

Use a “for” loop count backwards the even numbers from 10 to zero,

A
95
Q

Visualise a “for” loop code that will output each element of an array “arr” to the console:

A
96
Q

Visualise a nested “for” loop code that will output each element of a multidimentional array “arr” to the console:

A
97
Q

What is a “Do… while” loop?

A

A loop that will first do one pass of the code inside the loop no matter what, and then continue to run the loop while the specified condition evaluates to true.

98
Q

What is recursion?

Viaualise the recursive version of the below code?

A

Recursion is the concept that a function can be expressed in terms of itself.

Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0), otherwise they can never finish executing.

99
Q

What does the code, seen below, do?

A

It accepts two arguments and looks up for the “name” argument in an array of objects. If the name is found, then it accertains if the “prop” argument is one of the properties of the object with that name.

100
Q

In JS, what does the Math.random() method do?

A

It generatesa random decimal between 0 (inclisuve) and 1 (exclusive).

101
Q

Visualise a JS function that returns a random whole number between 0 and 9 (inclusive).

A
102
Q

Visualise a function called randomRange that takes a range myMin and myMax and returns a random whole number that’s greater than or equal to myMin, and is less than or equal to myMax.

A
103
Q

What does the parseInt() function do?

A

The parseInt() function parses a string and returns an integer.

The below function converts the string 007 to the integer 7. If the first character in the string can’t be converted into a number, then it returns NaN.

104
Q

In the line of code below, what does the radix argument represent?

A

The radix variable says that the number in the string argument is in the numeric system X, or base X. This can be any number between 2 and 36. This example converts the string 11 to an integer 3.

105
Q

What is the Conditional (Ternary) Operator?

A

It is a one line if-else expression.

The syntax is a ? b : c, where a is the condition, b is the code to run when the condition returns true, and c is the code to run when the condition returns false.

106
Q

Visualisy the below code written in ternary form.

A

Remember to use proper indentation.

107
Q

Visualise code that uses recursion to produce an array that counts down from an argument “n” all the way to 1.

A
108
Q
A