Chapter 1 Values, Types, and Operators Flashcards
What are the six basic types of values in JavaScript
numbers, strings, Booleans, objects, functions, and undefined values.
What are the three special values in JavaScript that are considered numbers but don’t behave like normal numbers.
The first two are Infinity and -Infinity, which represent the positive and negative infinities. Infinity - 1 is still Infinity, and so on. Don’t put too much trust in infinity-based computation. It isn’t mathematically solid, and it will quickly lead to our next special number: NaN.
NaN stands for “not a number”, even though it is a value of the number type. You’ll get this result when you, for example, try to calculate 0 / 0 (zero divided by zero), Infinity - Infinity, or any number of other numeric operations that don’t yield a precise, meaningful result.
What does the typeof operator produce?
It produces a string value naming the type of the value you give it. console.log(typeof 4.5) // → number
There is only one value in JavaScript that is not equal to itself, what is it?
NaN
NaN is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations.
What is special about null and undefined?
They are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.
Many operations in the language that don’t produce a meaningful value yield undefined simply because they have to yield some value.
The difference in meaning between undefined and null is an accident of JavaScript’s design, and it doesn’t matter most of the time. In the cases where you actually have to concern yourself with these values, I recommend treating them as interchangeable
What is type coercion?
When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect.
console.log(8 * null) // → 0 console.log("5" - 1) // → 4 console.log("5" + 1) // → 51 console.log("five" * 2) // → NaN console.log(false == 0) // → true
What is the difference between confirm() and prompt()?
confirm() has a true or false answer
prompt() is an open ended question
An important property of functions is that the variables created inside of them, including their parameters, are local to the function. What does this mean?
The variable in a function will be newly created every time the function is called, and these separate incarnations do not interfere with each other.
What are variables declared outside of a function called?
global variables
It is possible to access such variables from inside a function, as long as you haven’t declared a local variable with the same name.
Give an example of function declaration.
function square(x) { return x * x; }
The statement defines the variable square and points it at the given function.
What is a side effect of defining a function through function declaration?
console.log(“The future says:”, future());
function future() { return "We STILL have no flying cars."; } This code works, even though the function is defined below the code that uses it. This is because function declarations are not part of the regular top-to-bottom flow of control. They are conceptually moved to the top of their scope and can be used by all the code in that scope. This is sometimes useful because it gives us the freedom to order code in a way that seems meaningful, without worrying about having to define all functions above their first use.
What is the Javascript call stack?
The order in which Javascript code is implemented
What does it mean when the the computer fails with a message like “out of stack space” or “too much recursion”
Storing functions in a call stack requires space in the computer’s memory. When the stack grows too big, the computer will fail with these messages. This can also happen if you write an infinite loop as the computer does not have infinite memory. Also known as “blow the stack”
The following code is allowed and executes without any problem:
alert(“Hello”, “Good Evening”, “How do you do?”);
The function alert officially accepts only one argument. Yet when you call it like this, it doesn’t complain.
Explain why
JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters simply get assigned the value undefined.
What is closure in Javascript?
This feature—being able to reference a specific instance of local variables in an enclosing function—is called closure.
function wrapValue(n) { var localVariable = n; return function() { return localVariable; }; }
var wrap1 = wrapValue(1); var wrap2 = wrapValue(2); console.log(wrap1()); // → 1 console.log(wrap2()); // → 2
What is Recursion?
When a function calls itself
function power(base, exponent) { if (exponent == 0) return 1; else return base * power(base, exponent - 1); }
console.log(power(2, 3)); // → 8
What is a pure function?
A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code