Fundamentals Flashcards

1
Q

What 3 keywords are used to declare variables?

A

let, var & const

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

What is the main difference between let/const and var?

A

let is block-scoped (only exists within the lifetime of a scope) & cannot be redeclared. Var is global scoped, and can be redeclared. Let is preferable to var.

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

What is the difference between let & const?

A

let declares a variable, const declares a constant. The value associated with a constant cannot be changed - it is immutable.

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

Why should const be the default keyword?

A

Using const offers a guarantee that is a variable should not be changed, it won’t be, which makes code more secure. Let then becomes used deliberately for variables that you plan to alter.

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

What are the basic/scalar types?

A
number
string
boolean
undefined
object (rarely used)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two simple types of data collections?

A

object

array (/ array-like)

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

What does the term ‘execute’ mean?

A

To execute means to activate the action defined by the code.

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

What is a function?

A

A function is a scope of code to be executed when it is invoked.

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

What format is number data-type stored as?

A

64-bit floating point numbers. There are no explicit scalar integer types in TS/JS.

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

How would you declare a constant variable “foo” with a value of 5?

A
const foo = 5; // implicit
const foo: number = 5; // explicit
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between implicit and explicit typing?

A

The type of the variable if implicit is inferred from the value assigned upon initialisation, if explicit is the specified type.

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

What is a literal?

A

Any value that is not stored in a variable (e.g. 5, “hello”, true).

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

Define a variable “bar” that can be a string or a number, and initialise with value 5.

A

let foo: string | number = 5;

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

What does Truthy & Falsy mean?

A

Truthy values are values of any type that, in any operation that requires a boolean value, evaluate as True. Falsy values in the same situation evaluate to false.

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

What are the standard Falsy values?

A

false, 0, “”, undefined, null, NaN

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

If foo is a number variable of 5, what would foo.toFixed(2) produce?

A

toFixed returns a string version of a number to fixed decimal places.
foo.toFixed(2) === “5.00”;

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

If foo is a number variable of 555, what would foo.toPrecision(2) produce?

A

toPrecision defines the precision (rounding) of a number.

foo.toPrecision(2) === “560”;

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

what does the toString() method do?

A

Returns a string version of the variable.

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

If foo is a number variable of 5, what would foo.toString(2) produce?

A

toString returns a string version of the number in this case, with the number defining the base (default base 10). In this case it would be “101”;

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

What is a method?

A

A method is a function bound to an object/class.

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

What are the prototypes associated with each scalar type?

A

Number, String, Boolean, Object, Array

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

If foo is a number variable of 5, what would Number.isInteger(foo) produce?

A

Number.isInteger() is a static method on the Number wrapper class that evaluates if a number is an integer. Number.isInteger(foo) === true;

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

How are string literals represented?

A

With single or double quotes.
I recommend using double quotes for strings, and single quotes for chars, as this is standard for other programming languages.

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

What are escape characters?

A

characters in a string that are “escaped” with a backslash () that do something to that string representation in the browser. e.g. \n will create a newline.

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

What are template literals?

A

Template Literals are represented by backticks and evaluate values in ${} to string values within the literal.
e.g. Hello ${person.name}

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

What String methods can be used to return a string variable to lower and upper case?

A

foo. toUpperCase();

foo. toLowerCase();

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

What property defines the length of a string?

A

foo.length;

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

What method can be used to separate a string into an array?

A

foo.split(“ “) - this will split the words by spaces.

29
Q

What method can be used to return a proportion of a string

A

foo.slice(0, 5); – this would return “Hello” if foo is “Hello World”

30
Q

What method can be used to find the index of a character within a string

A

foo. indexOf(“a”);

foo. lastIndexOf(“a”); – searches from end first

31
Q

What method can be used to replace instances of a string with a different string?

A

foo. replace(“a”, “b”) // for 1

foo. replaceAll(“a”, “b”) // for all

32
Q

What are the arithmetic (number) operators

A
\+ -> addition
- -> subtraction
* -> multiplication
/ -> division
% -> modulus or remainder of division
\++ -> increment (x = x + 1)
-- -> decrement (x = x - 1)
33
Q

What are the assignment operators?

A

Assignment operators define how a value on the right hand side, when evaluated, is assigned to the left hand variable.
= -> is assigned to
+= -> addition assignment (x = x + y)
-= -> subtraction assignment (x = x - y)
*= -> multiplication assignment (x = x * y)
/= -> division assignment (x = x / y)
%= -> modulus assignment (x = x % y)

34
Q

What are the 2 valid string operators?

A

+ -> String concatentation (“hello “ + “world”)

+= -> addition assignment (foo += “world”

35
Q

What are the comparison operators

A
Operators that compare 2 values and output a boolean result.
===  -> is equal to
!==  -> is not equal to
>  -> is greater than
>=  -> is greater than or equal to
<  -> is less than or equal to
<= -> is less than or equal to

(== -> is loosely equal to… but don’t use)

36
Q

What are the logical operators?

A

&& -> Logical AND (evaluates to True if both LH & RH are truthy)
|| -> Logical OR (evaluates to true if one or both values are truthy)
! -> negates/inverts the boolean value of an expression (e.g. !true === false)

37
Q

How do logical AND & OR operators work

A

They execute the code on either side dependent on whether either side is truthy or falsy.

38
Q

How can logical assignment be implemented?

A

Logical or can be used to only assign
let foo = “” || “Hello World”; // outputs Hello World.
let foo ||= “Hello World”; // if foo is falsy

let bar = "I think" && "therefore I am"; // outputs "therefore I am"
let bar &&= "therefore I am" // if bar is truthy
39
Q

What is the nullish operator?

A
The nullish operator (??) can be used to execute/evaluate the right hand side if the left hand is not nullish (null or undefined ONLY). There is also a nullish assignment operator.
let bar = "I think" ?? "therefore I am";
let foo = null ?? "I won't be executed";
40
Q

What does the typeof operator return?

A

the typeof operator will return the basic scalar type (object if not scalar) in string form of a value.
typeof “hello” === “string”;

41
Q

What does the instanceof operator return?

A

the instance of operator evaluates whether or not an object is an instance of a class.

42
Q

What are the main non-scalar Classes used in Typescript

A
Array
Object
Date
JSON
Error
Global/window (node/browser)
Regexp (Regular Expressions)
43
Q

What is the syntax of a regular expression literal

A

/Hello World/igm -> will match a sequence of characters (in this case, exactly).

44
Q

What is a conditional

A

A conditional defines a scope of code to be executed if the input value evaluates to true or truthy.

45
Q

What is the syntax of an IF statement

A
if (condition) {
  // if condition is true, this code will execute
}
46
Q

What is the syntax of an IF ELSE statement

A
if (condition) {
  // if condition true
} else {
  // if condition false
}
47
Q

What will be the output of this conditional be? If (!!true) { console.log(“Option A”); } else { console.log(“Option B”) }

A

“Option A” -> !!true is double negation so evaluates to true, therfore the if codeblock will execute

48
Q

What is a loop?

A

A code block that repeatedly executes all the while an associated condition is true.

49
Q

What is the syntax of a WHILE loop?

A
while (condition) {
   // while condition is met
}
50
Q

What is the syntax of a DO WHILE loop?

A
do {
  // will run at least once and while condition is met
} while (condition)
51
Q

Will the following execute, if so how many times?

while (0) { console.log(“hey”) }

A

No, as 0 is falsy.

52
Q

Will the following execute, if so, how many times?

do { console.log(“hey”); } while (0)

A

Yes, once. Do executes the code once, and the condition associated with while is falsy.

53
Q

What type of loop is a FOR loop

A

A FOR loop is an iterative loop. (While is purely conditional)

54
Q

What is the syntax of a FOR loop

A
for (start; end; onEach) {
  // executes on each iteration
}
55
Q

Write a for loop that logs out the first 5 characters in a string variable “foo”.

A
for (let i = 0; i < 5; i++) {
   console.log(foo[i]);
}
56
Q

What does a FOR IN loop do?

A

Loops through the property keys in an object.

57
Q

What is the Typescript syntax of a FOR IN loop?

A
for (let key in obj) {
  const typedKey = key as keyof typeof obj;
  console.log(obj[key]);
}
58
Q

What does a FOR OF loop do?

A

Loops through the property values in an object, or values of an array. NOTE: With an object, the method must be defined, as the values are not ordered. It is best not to use this with an object, there are ways around it.

59
Q

What is the syntax of a FOR OF loop?

A

let foo = [1,2,3];
for (let val of foo) {
console.log(val)
}

60
Q

What is the Date library?

A

The Date library gives a series of methods that converts the number of milliseconds since 1970 into dates and times.

61
Q

Which Date method gives the number of milliseconds since 1970.

A
const foo = new Date();
foo.getTime(); // 1617545873697 or similar
62
Q

What are the two static methods available on the JSON class library?

A

JSON.parse() -> Transforms JSON to object

JSON.stringify() -> Transforms object to JSON.

63
Q

What does the keyof operator do?

A

The keyof operator takes an object type and produces a string or numeric literal union of its keys.

64
Q

What does the tsconfig file do?

A

Defines how Typescript is compiled.

65
Q

What does Typescript compile to?

A

Javascript.

66
Q

What does a bundler do?

A

A bundler simplifies file directories for runtime. Bundlers include Webpack, snowpack, parcel & rollup.

67
Q

How do you initialise a tsconfig file?

A

tsc –init (or touch tsconfig.json for blank slate). NOTE: use my personal tsconfig and save it as a snippet.

68
Q

How do you initialise a linter for Typescript

A

npx eslint –init

Then follow instructions and select typescript, and select style guide.