JS Flashcards

1
Q

En uppgift =

A

en funktion

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

Vad är nackdelen med att skapa funktioner inuti loopar?

A

Det använder mycket minne. Skapa funktioner utanför loopar.

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

Vad är JavaScript?

A

Ett mångsidigt programmeringsspråk som används för webbutveckling.

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

Vad är en variabel?

A

En behållare för att förvara data/värde

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

Hur deklarerar man en variabel i JavaScript?

A

Via nyckelorden var, let, eller const.

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

Vad är skillnaden mellan var, let, och const?

A

var har functionscope, let och const har blockscope.

constvariabler kan inte ändras efter att de deklarerats.

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

Vilka 8 datatyper har JavaScript?

A

number, bigint, string, boolean, null, undefined, object och symbol

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

Vad används typeof operatorn till?

A

Ta reda på datatypen av ett värde eller en variabel.

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

Kommentera i JavaScript?

A

// för enradiga kommentarer

/* */för flera rader

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

Vad är en funktion i JavaScript?

A

Ett block av återanvändbar kod skriven för att utföra en viss uppgift.

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

Hur deklareras en funktion?

A

Via nyckelordet function.

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

Vad är en function parameter?

A

En variabel som används för att skicka data in till en funktion.

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

Vad är ett return statement i en funktion?

A

Det specificerar värdet som ska returneras av funktionen

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

Vad är en villkorssats i JavaScript?

A

Villkorssatser, som if, else if, och else, gör att man kan villkora delar av kod.

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

Vad är en operator i JavaScript?

A

En symbol som används för att operera på variabler och värden.

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

Vad gör === operatorn?

A

Strikt equality. Både typ och värde.

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

Vad är en array?

A

En datastruktur som innehåller en samling värden.

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

Hur kommer man åt värden i en array?

A

Via index, startar på 0.

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

Vad är ett JavaScript object?

A

En samling key-value par.

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

Hur skapar man ett object i JavaScript?

A

{}
Eller constructor-funktioner

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

Vad är en JavaScript-loop?

A

Loopar, som for och while, kan exekvera kod upprepade gånger.

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

Hur kan man bryta en loop?

A

break

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

Vilket syfte fyller switch i JavaScript?

A

switch används för villkorade förgreningar baserat på ett värde.

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

Vad är en anonym funktion?

A

En funktion utan namn

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

vad är en callback function?

A

En funktion som skickas som argument till en annan funktion.

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

Vad betyder scope?

A

Var variabler och funktioner är åtkomliga i koden.

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

Vad är ett closure?

A

En funktion med åtkomst till sitt eget scope och den omgivande funktionens scope.

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

Vad är en event handler?

A

En funktion som svarar på händelser som click eller knapptryck.

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

Vad är asynkron programmering?

A

Asynkron programmering tillåter icke-blockerande exekvering av kod.

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

Vad är promises?

A

Objekt som representerar det eventuella resultatet av en asynkron operation.

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

Vad är nyckelordet this?

A

Det refererar till det nuvarande objektet. Ofta i metoder i ett objekt.

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

Vilket syfte fyller console.log() ?

A

Skriver output till konsolen för debugging.

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

Hur kan man definiera en iife?

A

( function () {…} )();

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

Hur aktiverar man ”modern mode” i JavaScript?

A

”use strict”

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

for…of

A

Loops over array or other iterable

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

for…in

A

Loops over object properties

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

Where can JS run?

A

Anywhere there’s a JS-engine

38
Q

The three steps of executing JS?

A
  1. Engine parses script (where’s the optimization and JIT-compiling?)
  2. Converts to machine code
  3. Machine code runs, pretty fast
39
Q

JS statements =

A

syntax constructs and commands that perform actions

40
Q

Difference between statements and expressions

A

Statements = control flow, variable assignment etc.
Expressions produce values.

41
Q

How to strict mode?

A

“use strict” at top of script OR use classes or modules

42
Q

when to use const UPPER_CASE ?

A

Storing hard to remember values, known prior to execution.

43
Q

3 things good variable names are ?

A
  1. clean,
  2. obvious and
  3. descriptive of the data they store
44
Q

What is immutable code?

A

It contains no datastructures that can be modified

45
Q

How to write immutable code?

A

Instead of modifying data, create new data with desired changes

46
Q

Four rules for good variable names?

A
  1. Human readable
  2. No abbreviations
  3. Maximize descriptiveness + consicesness
  4. Be consistent, “newUser” implies “user”
47
Q

What 2 bugs are related to typeof?

A
  1. typeof null returns Object
  2. typeof [any function] returns function (which isn’t one of the 8 types in JS)
48
Q

What does unary + do?

A

Number conversion.

49
Q

What does binary + do?

A

Sum numbers or concatenate strings (if one operand is string).

50
Q

What’s an operand?

A

A value an operator is applied to.

51
Q

How to give precedence to operators?

A

(expression to give precedence)

52
Q

What’s the difference between n++ and ++n?

A

Postfix (n++) increments after returning n
Prefix (++n) increments before returning n

53
Q

What 2 values shouldn’t be used in comparisons?

A

Null and undefined

54
Q

When to use the ternary operator?

A

To return one value or another depending on one condition

55
Q

When to use if statements?

A

To execute different branches of code

56
Q

What does || do?

A

OR operator finds first truthy value and returns it

57
Q

What does && do?

A

AND operator finds first falsy value and returns it

58
Q

What does ! do?

A

NOT operator converts to boolean and returns the opposite !0 == true

59
Q

Use case for !! ?

A

Double NOT, simple boolean conversion

60
Q

What does ?? do?

A

Nullish coalescing, returns first defined value

61
Q

Arity of reduce()?

A
  1. Callback for each element
  2. Starting point for final output
62
Q

Arity of the callback in reduce()?

A
  1. Starting point by reference (like ‘sum’) after first iteration it refers to the result from previous iteration
  2. Current element
63
Q

Method for preventing adding new properties on an object?

A

Object.preventExtensions(obj)

64
Q

Method for preventing adding/removing properties on an object?

A

Object.seal(obj)

65
Q

Method for preventing adding/removing/changing properties on an object?

A

Object.freeze(obj)

66
Q

for-loop syntax?

A
for(  let i = 0; i < numberOfIterations; i++) {
// loop body
}
67
Q

Use case for infinite loops?

A

In combination with break. Great for checking conditions in many places in loop body,

68
Q

What does continue do?

A

Stops the current iteration in a loop and jumps to the next.

69
Q

Use case for labeling loops?

A

To be able to use break and continue in nested loops.

70
Q

How to label loops?

A
labelName: for(…) {
  // …
}
71
Q

No more than___ lines of code without blank line.

A

9

72
Q

Two ways of avoiding too much nesting?

A
  1. Negative ifs and using continue
  2. Nested else can be avoided with return
73
Q

4 things that warrants a comment?

A
  1. Describe architecture
  2. Documentation of function parameters and usage
  3. Why is the task solved this way
  4. Subtle or counter intuitive features
74
Q

Object property value shorthand?

A
function makeUser (name, age) {
    return {
        name,
        age
    }
}
75
Q

How to test for existence of a property in an object?

A

With in operator:
property in object

76
Q

How to loop over object properties?

A
for (let prop in obj) {
  // loop body
}
77
Q

What does a variable assigned to an object store?

A

An address to the object (in memory). I.e a reference to the object

78
Q

How to clone an object?

A
Object.assign(dest, …sourceObjects)
79
Q

When are objects garbage collected in JS?

A

When it isn’t reachable anymore. I.e when there is no longer any existing reference to that object.

80
Q

How to make method calls chainable?

A

Make every method return this

81
Q

How does JS handle this?

A

this is not bound
this is evaluated during runtime depending on context

82
Q

What´s this in an arrow function?

A

Arrow functions inherit this from the outer function.

83
Q

What’s a constructor function?

A

A function that is used to create new objects.

84
Q

Give two examples of built-in constructors?

A
  1. new Date()
  2. new Set()
85
Q

What three things happen when a constructor function is called with the new keyword?

A
  1. A new empty object is created and assigned to the this keyword.
  2. The body of the constructor function is executed (with this referring to the empty object, writing properties into it).
  3. this is returned
86
Q

What is the main purpose of constructor functions?

A

Implement reuseable code for object creation.

87
Q

What to use instead of constructor functions for more complex objects?

A

Class syntax.

88
Q

What does ?. do?

A

Optional chaining. If the value before ?. is null or undefined evaluation stops and undefined is returned.

89
Q

What is the use case for the symbol type in JS?

A

Symbols are used as unique keys in objects.

90
Q

Symbols … a uniqe identifier

A

represent

91
Q

How to create a new symbol?

A
const mySymbol = new Symbol()
92
Q

Can symbol properties be accessed by accident?

A

No