How JS works Flashcards

1
Q

What is a block in JS?

A

A chunk of code between curly brackets {}.
N.b. this does not mean objects are blocks!
Think functions, if statements, for loops etc.

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

What is the difference between var, let and const.

A
  1. let and const are newer (ES6) than var.
  2. let and const are block scoped while var is globally or function scoped.

i.e. if a variable is declared with let or const within a block, it cannot be accessed outside of this block.

With variables declared using var, this only applies to functions i.e. only var variables declared within functions cannot be accessed outside functions.

  1. let can be updated but not redeclared, var can be redeclared and updated, const cannot be updated or redeclared.
DECLARATION: var x = "y"
REDECLARATION: var x = "z" OK 
UPDATE: x = "b" OK
  1. const variables must be initialized on declaration whereas var and let need not be initialized.
  2. They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is ‘hoisting’?

A

In JS a variable can be declared after it has been used.

By default, JS compiler will move all declarations to the top of the current scope e.g.

//your code
console.log(x);
let x = 3;

//After hoisting
let x;
console.log(x);
x = 3;

(n.b. in this instance an error will be thrown because let and const variables aren’t initialized but if it was var, the console.log would print undefined)

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

What effect does adding ‘use strict’ to the top of a JS file have on your code?

A

It turns on strict mode. Strict mode changes some previously-accepted mistakes into errors e.g.

It makes it impossible to accidentally create global variables. e.g. x = 3 (no var, no const, no let!)

It makes assignments which would otherwise silently fail to throw an exception. e.g. var Infinity = 3’. (n.b. const and let would prevent this)

It makes attempts to delete undeletable properties throw e.g. delete Object.prototype would fail.

It requires that function parameter names be unique

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

How does a JavaScript engine work?

A

A JavaScript engine takes the source code, breaks it up into strings—a.k.a. lexes it, takes those strings and converts them into bytecode that a compiler can understand, and then executes it.

The goal of a JavaScript engine’s code parsing and execution process is to generate the most optimized code in the shortest possible time.

As such there is an interpreter and a JiT compiler. While the interpreted program is being run, the JIT compiler determines the most frequently used code and compiles it to optimized, faster machine code.

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

What’s the difference between a compiler and an interpreter?

A

An interpreter will run through code line by line and execute each command.
A compiler will take all of the code and convert it into optimised machine code which can then be executed.

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

What is Just In Time Compilation?

A
  1. A method for improving the performance of interpreted programs
  2. Translation to machine code that is being done during the execution of a program. Meaning, at run time, as opposed to prior to execution.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a JiT Compiler?

A

A feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead.

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

What is a JavaScript runtime environment and how does it differ from a JavaScript engine?

A

A JavaScript engine translates your code into machine code and executes this code when the code is run using JiT compilation.

A runtime environment contains everything needed to execute a program e.g. a Browser is a JS runtime environment with a call stack, memory heap, Web APIs, Event Loop and Callback queue. Nodejs is another JS runtime which provides a single-threaded non-blocking event loop, and a low-level I/O API.

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

What are the primitive values in JavaScript?

A

String, Number, undefined, null, Boolean, Symbol

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

What’s the difference between primitive and reference values?

A

If a variable is declared with a primitive and another variable is declared and assigned to this first variable, the space in memory of this second variable will hold a copy of this primitive value, rather than a reference to the memory location of the first variable (as with reference values ie. all other objects).

let p = "suit";
let c = p;
c = "has" // p would still be "suit"
let p = { "name" : "Jill" }
let c = p;
c.name = "Kay" // p would now be { "name": "Kay" }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does a JavaScript engine’s garbage collector do?

A

The garbage collectors is a background process in the JavaScript engine.

It monitors all objects and removes any that are no longer needed i.e. they are not being used in current execution and there is no reference to them from the current execution.

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