- declare
global variable inside the function: using window object(e.g.window.value = 10;
)
all data types are object.
Primitive data types
- Numbers: All numbers in JavaScript are represented as
floating-point values. - String
- Boolean
null
and undefined
undefined
null
var, let, const
- var:
not block scoped . It has scope in functions.let
andconst
are bound to the block. - let: valid within {}. block scoped. always use
code
to avoid scope errors. - const: cannot be reassigned
Difference between == and ===
- identity operator ===: no type conversion so types must be the same to be True
- equality operator ==: do type conversion first before comparison
strict mode
Strict mode is a way to introduce better error-checking into your code. When you use strict mode, you cannot, for example, use implicitly declared variables
Declare:
"use strict";
Benefits: easier debug, prevent accidentally global etc.
NaN
The NaN property represents a value that is "not a number".
NaN
has type Number
.Closure
closure is an inner function that has access to the outer function's variables-scope chain.
The closure has three scope chains:
- own scope (variables defined between its curly brackets) (even after outer function return
- outer function's variables
- global variables
- outer function's parameters
Sinon  
Standalone test spies, stubs and mocks for JavaScript.
- Fakes  
[View Details on Official Website] - a
fake is a Function that records arguments, return value, the value of this and exception thrown (if any) for all of its calls. - It can be created with or without behavior; it can wrap an existing function.
- Creating a fake
// create a basic fake, with no behavior var fake = sinon.fake(); fake(); console.log(fake.callCount); // 1
- a
- Spies  
[View Details on Official Website] - A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls.
- Creating a spy
// Creates an anonymous function that records arguments, this value, exceptions and return values for all calls. var spy = sinon.spy(); // Spies on the provided function var spy = sinon.spy(myFunc); // Creates a spy for object.method and replaces the original method with the spy. var spy = sinon.spy(object, "method");
- Stubs  
[View Details on Official Website] - Test stubs are functions (spies) with pre-programmed behavior.
- When to use stubs?
- Control a method's behavior from a test to force the code down a specific path. Examples include forcing a method to throw an error in order to test error handling.
- When you want to prevent a specific method from being called directly (possibly because it triggers undesired behavior, such as a XMLHttpRequest or similar).
- Creating a stub
// Creates an anonymous stub function var stub = sinon.stub(); // Replaces object.method with a stub function. An exception is thrown if the property is not already a function. var stub = sinon.stub(object, "method");
- Mocks  
[View Details on Official Website] - Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations.
- When to use Mocks?
- Mocks should only be used for the method under test. In every unit test, there should be one unit under test.
- If you want to control how your unit is being used and like stating expectations upfront (as opposed to asserting after the fact), use a mock.