Introduction to JavaScript & Data Types Flashcards

1
Q

What is a runtime environment and its use?

A

A runtime environment turns an application from a set of instructions into something that can do actual work.

It is an execution environment that allows an application program to access the machine’s system resources such as networking infrastructure, RAM, sensors, or GPU.

It provides the tools the application needs to let the operating system understand what it is doing and interact with the outside world.

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

What is the purpose of compilers and interpreters? What’s the difference between the two?

A

Compilers and interpreters help convert programming languages into a form that the computer can understand (e.g. ones and zeroes).

Compilers produce an output file that the computer can run directly.

Interpreters run the interpreted code directly instead of translating it into 1s and 0s.

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

What type of compiler/interpreter do most modern JS engines use?

A

JavaScript is an interpreted language. However, most modern JS engines employ a special type of compiler called Just In Time (JIT) compiler.

JIT compiler is a way of executing code that involves compilation DURING the execution of the program.
i.e. compiles the code while the programming is running rather than before.

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

What is an API and how is it related to a runtime environment?

A

An API describes the scheme (plan) and format that a programmer can use to securely access an operating system’s resources.

The compiler/interpreter and the OS’s APIs together make up a runtime environment. They provide the tools that an application needs to run.

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

What are JavaScript’s major runtime environments?

A

The browser (frontend) and Node.js (general).

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

How does Javascript interact with the web browser?

A

The web browser is JavaScript’s original and dominant runtime environment. Almost every browser has a JS engine built into it.

JS interacts with the DOM API to manipulate the structure and appearance of a web page.

JS also interacts with the XHR (XMLHttpRequest) interface and the Fetch API to communicate with a server.

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

What is Node.js? How was it developed? What does it allow JS to do?

A

Node.js is a runtime environment that turns JS into a general-purpose programming language that can run applications on almost any system.

Node.js allows Javascript to:

(1) Read/write disk files (disk I/O)
(2) Read/write via the terminal (standard I/O)
(3) Send/receive messages over a network (network I/O)
(4) Interact with a database

The creators of Node.js took the open-source Chrome V8 JS engine and added APIs and tools required for desktop and server computing.

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

What are Javascript’s two types of methods?

A

Instance and static methods.

Instance methods uses String.prototype.method() and requires an object of its class to be called.
e.g.
> ‘Hello, ‘.concat(‘Bob!’)
> ‘Hello, Bob!’

Static methods uses String.method() and does not require creating an object of its class.
e.g.
> String.fromCharCode(97)
> ‘a’

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

What are the 7 primitive data types?

A

String, Boolean, Number, Null, Undefined

Symbol and BigInt were introduced in ES6, not used often

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

What is the usage of the backslash character?

A

The backslash, or escape character (), tells the computer that the next character isn’t syntactic, but part of the string.

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

What is the difference between undefined and null data types?

A

Null and undefined both represent an absence of a value, but null is an INTENTIONAL absence, usually assigned by design.

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

How does JavaScript implicitly coerce with the == non-strictly equal operator?

A

When comparing numeric and non-numeric values with the non-strictly equal operator ==, JS will coerce the non-numeric value (string, booleans) to numeric.

e.g. 
> 1 == true 
// true because true will be coerced into a number
e.g. 
> undefined == null 
// true because 0 == 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between explicit and implicit type coercion?

A

Explicit type coercion lets you decide what to do with the data type whereas implicit coercion lets the JS engine choose.

e.g. Explicit coercion ‘1’ + ‘2’
> Number (‘1’) + Number (‘2’) = 3
// changes string to number

e.g. Implicit coercion ‘1’ + ‘2’
> ‘1’ + ‘2’ = ‘12’

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

What is an expression? A statement?

A

An expression is anything that Javascript can evaluate to a value, even if the value is undefined/null.

Statements refer to syntactic unit of code that expresses an action for the computer to perform. Statements always evaluate as undefined. e.g (console.log). In practice, most programmers use the term statement rather loosely.

Expressions can be part of a statement, but not all statements are expressions.

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

What do identifiers refer to in JS?

A
Variable names (let and var)
Constant names (const)
Objects' property names
Function names
Function parameters
Class names
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a variable’s scope?

A

A variable’s scope determines where it is available in a program. The location where you declare a variable determines its scope. A variable can be declared globally or locally.

let and const variables have block scope, which means it is only available between a pair of curly braces (if statements and for/while, loops).

e.g Block Scope
> if ( expression) {
> let a = 'foo'  'bar'
> }

Note: Object literals and class definitions have {} but are not considered block scope

17
Q

If a variable is undeclared, what is its scope?

A

Javascript has some bizarre rules when working with undeclared variables. The most notable rule is that all undeclared variables have global scope: they ignore block and function scope entirely.

Undeclared variables are variables assigned without using const, let or var.

18
Q

What are a few examples of a computer’s input and output sources?

A

Input sources: mice, keyboard, disk, network, environmental sensors

Output sources: screen, log, network

A computer needs to take input from some source, perform some actions on that input, and then provide output. This input/output cycle is at the heart of every computer program.

19
Q

What’s the difference between a non-mutating and a mutating method?

A

A non-mutating operation preserves the previous data value.
e.g. name.toUpperCase() will not change the name string

A mutating operation will change the previous data value.
e.g.
> let oddNum = [1, 3, 5, 7, 9]
// oddNum.pop() will permanently change oddNum array

Mutation is a concern when dealing with arrays and objects, but not with primitive data values like numbers, strings, booleans. Primitive values are immutable.

20
Q

What is the key difference between function declaration and function expressions?

A

You cannot invoke a function expression before it appears in your program. You can, however, call a function declaration before it is declared in the program.

Function expressions also use arrow syntax and variable terms (const, let, var)

21
Q

How is the short circuit evaluation mechanism used for operators?

A

Javascript short circuits an && or || expression by terminating the evaluation as soon as it determines the first expression.

e.g. isRed(item) && isPortable(item)
JS will evaluate if item is red. If it is not red (false), then the second expression, isPortable will not be evaluated

e.g. isGreen(item) || hasWheels(item)
JS will evaluate if item is green. If it is green (true), then the second expression, hasWheels will not be evaluated.

22
Q

What are examples of falsy values?

A
false
the number 0
an empty string (' ')
undefined
null
NaN

Note: Arrays are truthy!

23
Q

What are the operator precedence rules?

A

Comparison: <=, , >=
Equality: ==, !=
Logical AND: &&
Logical OR: ||

24
Q

What is the syntax for the ternary operator?

A

It uses a combination of ? and : symbols to write a short if/else statement.

condition ? if true : if not true

25
Q

What is the key difference between parseInt() and Number() methods?

A

parseInt() converts strings to numbers even if it contains non-numeric characters as long as the string BEGINS with a digit.

Number() will only convert a string to number if it only contains numeric characters.

26
Q

What is the difference between ‘pass by reference’ and ‘pass by value’?

A

‘Pass by value’ and ‘pass by reference’ are both strategies that describe the affect of function arguments.

‘Pass by value’ mean that when you pass in a variable as an argument for a function call, the function can’t do anything that changes the original variable to a different value. In JS, primitive data types passed in are immutable.

‘Pass by reference’ mean that you can affect the variable passed in as an argument for a function call. In JS, arrays and objects are mutable by certain methods (e.g. push, pop, shift, unshift) but not by other methods that create a new array (e.g. concat, map).

27
Q

How can a variable be used as a pointer (aka references)?

A

JavaScript stores primitive values in variables, but uses pointers for non-primitive values like arrays and objects. For non-primitive values, the values get stored in a region of memory.

e.g.
> let arrayOne = [1, 2]
> let arrayTwo = arrayOne
> arrayOne.push(3, 4)
> arrayOne --> [1, 2, 3, 4]
> arrayTwo --> [1, 2, 3, 4]  // objects are mutable 

HOWEVER, reassignment of the array will not mutate:
> let arrayOne = [1, 2]
> let arrayTwo = arrayOne
> arrayOne = [‘a’, ‘b’] // complete reassignment
> arrayTwo –> [1, 2] // remains the same

28
Q

What is the fastest way to count a single character among a string of character?

A

> let count = string.split(‘’).filter(char => char === ‘x’).length

29
Q

What will the following code output?

console.log([1, 2, 3] + [4, 5]);

A
> 1,2,34, 5 
// JS converts array to strings then concatenates strings
30
Q

What are a few recommended steps to debugging a program?

A
  1. Reproduce the error: This is a good way to isolate the problem and understand how it relates to other moving parts.
  2. Determine the boundaries of the error: Tweak the data that caused the error and understand is there a range of data that affects the scope of the error.
  3. Trace the code: Read the code line by line
  4. Understand the problem well
  5. Implement a fix: There are often multiple ways
  6. Test the fix
31
Q

What are the 5 rules of local scope? Which ones pertain to function scope specifically?

A
  1. Inner scope can access outer scope variables
  2. Outer scope cannot access inner scope variables
  3. Inner scope variables will overshadow outer scope variables

Function Scope Specific:

  1. Peer function scopes do not conflict.
    i. e. Variables can be named the same in two different functions
  2. Nested functions have their own local scope
32
Q

What happens when you compare an object and arrays using == non-strictly equal operator?

A

When both operands are objects, == behaves exactly like the === operator; that is, it considers two objects equal only when they’re the same object:

e.g.
> let arr = []
> arr == [] –> false
> arr == arr –> true

e.g.
> ‘’== [] –> true, empty string == empty array
> ‘’ == {} –> false, empty string !== empty object

33
Q

How does JavaScript implicitly coerce with the + operator?

A

When using the + operator for non-string values and strings, Javascript will convert the non-string value into a string and the concatenate the non-string value to the string.

e.g
> ‘1’ + 2 –> ‘12’
// 2 is converted into ‘2’

When using the + operator for numbers and booleans/null, the non-numeric value will be converted to a number.

> 1 + true;       // 2
> 1 + false;      // 1
> true + false;   // 1
> null + false;   // 0
> null + null;    // 0
> 1 + undefined;  // NaN
34
Q

What are the two best practices on type coercion?

A
  1. Always use explicit type coercions (covered in the previous assignment)
  2. Always use strict equality operators (=== and !==).