Introduction to JavaScript & Data Types Flashcards
What is a runtime environment and its use?
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.
What is the purpose of compilers and interpreters? What’s the difference between the two?
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.
What type of compiler/interpreter do most modern JS engines use?
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.
What is an API and how is it related to a runtime environment?
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.
What are JavaScript’s major runtime environments?
The browser (frontend) and Node.js (general).
How does Javascript interact with the web browser?
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.
What is Node.js? How was it developed? What does it allow JS to do?
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.
What are Javascript’s two types of methods?
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’
What are the 7 primitive data types?
String, Boolean, Number, Null, Undefined
Symbol and BigInt were introduced in ES6, not used often
What is the usage of the backslash character?
The backslash, or escape character (), tells the computer that the next character isn’t syntactic, but part of the string.
What is the difference between undefined and null data types?
Null and undefined both represent an absence of a value, but null is an INTENTIONAL absence, usually assigned by design.
How does JavaScript implicitly coerce with the == non-strictly equal operator?
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
What is the difference between explicit and implicit type coercion?
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’
What is an expression? A statement?
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.
What do identifiers refer to in JS?
Variable names (let and var) Constant names (const) Objects' property names Function names Function parameters Class names
What is a variable’s scope?
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
If a variable is undeclared, what is its scope?
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.
What are a few examples of a computer’s input and output sources?
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.
What’s the difference between a non-mutating and a mutating method?
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.
What is the key difference between function declaration and function expressions?
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)
How is the short circuit evaluation mechanism used for operators?
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.
What are examples of falsy values?
false the number 0 an empty string (' ') undefined null NaN
Note: Arrays are truthy!
What are the operator precedence rules?
Comparison: <=, , >=
Equality: ==, !=
Logical AND: &&
Logical OR: ||
What is the syntax for the ternary operator?
It uses a combination of ? and : symbols to write a short if/else statement.
condition ? if true : if not true