JavaScript Flashcards

1
Q

What is ECMA?

A

ECMA = an organisation that defines standards for technologies

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

What is ECMAScript?

A

A specification developed by ECMA

JavaScript is a programming language that conforms to the ECMAScript specification

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

What are classes?

A

Classes are syntactic sugar over prototypes and prototypical inheritance.

Because JavaScript is dynamically typed, classes in JS differ to those in say C#.

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

What are Value (Primitive) Types?

A
Number 
String 
Boolean
Undefined
Null
Symbol

Primitives are copied by their VALUE.

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

What are Reference Types?

A

All reference types are essentially OBJECTS!

e.g. Functions & Arrays

Objects are copied by their REFERENCE

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

PRIMITIVE TYPES:

let x = 10;
let y = x;

x = 20;

What is the value of y?

A

Primitives are copied by their VALUE.

x = 20;
y = 10;

These variables are independent of each other.

With primitive types, the value 10 is stored inside x:
let x = 10
When we copy that var, the value that is stored inside it is copied into the new var
let x = 10;
let y = x; // y = 10;

x = 20;

This is not the case when we use reference types (objects)

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

REFERENCE TYPES:

let x = { value: 10 };
let y = x;

x.value = 20;

What is the value of y?

A

Objects are copied by their REFERENCE

let x = { value: 10 };
let y = x;
x.value = 20;
y = 20;

When an object is used, it does not get stored inside ‘x’ itself.

It gets stored somewhere else in memory & the address of that location in memory is what gets stored in the variable ‘x’.

e.g. 
let x = { value: 10 };

object gets stored in memory
1233 = {value:10}

SO:
x = 1234 (1234 holds a reference to that place in memory which holds the object)

therefore when assigning ‘y’ the value of ‘x’, we actually assign it the reference in memory

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

What is the difference between scope & closure in JavaScript?

A

In scope, the variables are temporary.
E.G.
- a function within a function
- child function has two variables x & y
- variables are re-created & re-initalised each time the child function is called
- after the function has completed, the variables in the child function die in the scope

In closure, the variables within the parent function stay initialised

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

What is classical inheritance?

we have prototypical inheritance in JS, NOT classical but it’s good to know the difference

A

Instead of redefining common methods, they can be defined and then inherited by other classes.

It allows us to re-use code easily & eliminate redundant code.

Reduces opportunity for bugs (no spaghetti code!).

Makes it easier to change/maintain code as you just need to look in one place to make adjustments.

E.G.

base/super/parent class
= Shape
has a function = startLocation()

derived/sub/child class
= Circle, Square, Triangle

inheritance relationship = IS-A

E.G. Circle IS-A Shape, and inherits it’s function

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

What is a prototype?

A

Every object in JS, except ___ has a prototype (parent).

This object inherits everything defined in the prototype.

Think of it as a starting base set up for all objects.

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

What are Falsy values?

A
undefined
null
0
false
''
NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Truthy values?

A

Anything that returns true from a boolean statement/is not falsy

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

What is coercion?

A

Implicit & Explicit

EXPLICIT:
var a = "42" //string
var b = Number(a);  // a becomes a number
IMPLICIT:
var a = "42" //string
var b = a * 1 ;  // 42 becomes the number because we perform a math operation on it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is scope?

A

Availability of variables and functions within local, global or lexical scope?

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

What are the two different types of equality?

A

Strict comparison === checks for value & type equality (no coercion)

Abstract comparison == checks for value equality only (with coercion allowed)

E.G:
var a = "42";
var b = 42;

a == b; // true
a === b; // false

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

What is an array?

A

An array is an object that holds values (of any type) in numerically indexed positions.

17
Q

Explain null & undefined:

A

UNDEFINED = something hasn’t been initialised with a value.

NULL = something is currently unavailable.

18
Q

What is strict mode?

A

The use strict literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake. For example, the following program will throw an error:

function doSomething(val) {
  "use strict"; 
  x = val + 10;
}`

It will throw an error because x was not defined and it is being set to some value in the global scope, which isn’t allowed with use strict The small change below fixes the error being thrown:

19
Q

What is a stack?

A

???

20
Q

What is a callback function?

A

A function that is passed to another function as an argument.

21
Q

How can you iterate over OBJECTS?

A

For…in loop

22
Q

How can you iterate over ARRAYS?

A

for loop, while loop

Higher order functions:

  • map
  • forEach
  • filter
  • indexOf
23
Q

What is a JSON?

A

JSON (JavaScript Object Notation) is a lightweight format that is used for exchange of data.

It is based on a subset of JavaScript language (the way objects are built in JavaScript).