What is a pure function ?

Apr 3, 2017

A pure function is a function which:

  • Given the same input, will always return the same output.
  • Produces no side effects.
  • Relies on no external mutable state.

  • References

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976

Categories : JavaScript

What is JavaScript Event Loop ?

Apr 3, 2017

JavaScript code runs single threaded. Javascript has a concurrency model based on an event loop. The event loop is in the heart of Node.js / Javascript - it is responsible for scheduling asynchronous operations. A very interesting property of the event loop model is that JavaScript, unlike a lot of other languages, never blocks. Handling I/O is typically performed via events and callbacks, so when the application is waiting for an IndexedDB query to return or an XHR request to return, it can still process other things like user input.

Two main parts of the event loop are the call stack and the message queue.

The call stack is a LIFO queue (Last In, First Out).

The Message Queue is also where user-initiated events like click or keyboard events, or fetch responses are queued before your code has the opportunity to react to them. Or also DOM events like onLoad.

Event Loop is a process that constantly checks whether the call stack is empty, and whenever it’s empty, it checks if the event queue has any functions waiting to be invoked. If it does, then the first function in the queue gets invoked and moved over into the call stack. If the event queue is empty, then this monitoring process just keeps on running indefinitely.

Async callbacks

An asynchronous callback function is just like any other function you’re used to writing in JavaScript, with the added caveat that it doesn’t get executed till later.

(function() {

  console.log('this is the start');

  setTimeout(function cb() {
    console.log('Callback 1: this is a msg from call back');
  }); // has a default time value of 0

  console.log('this is just a message');

  setTimeout(function cb1() {
    console.log('Callback 2: this is a msg from call back');
  }, 0);

  console.log('this is the end');

})();

// "this is the start"
// "this is just a message"
// "this is the end"
// "Callback 1: this is a msg from call back"
// "Callback 2: this is a msg from call back"

The use case of

setTimeout(() => {}), 0)

is to call a function, but execute it once every other function in the code has executed.

References

Categories : JavaScript

Same origin policy in JavaScript

Apr 1, 2017

The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin.

Two pages have the same origin if the protocol, port (if one is specified), and host are the same for both pages.

When using document.domain to allow a subdomain to access its parent securely, you need to set document.domain to the same value in both the parent domain and the subdomain. This is necessary even if doing so is simply setting the parent domain back to its original value. Failure to do this may result in permission errors.

  • References

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

http://lucybain.com/blog/2014/same-origin-policy/

Categories : JavaScript

Difference between JS Varaible that is null, undefined, undeclared

Apr 1, 2017

  • undeclared variables don’t even exist
  • undefined variables exist, but don’t have anything assigned to them
  • null variables exist and have null assigned to them

  • References

http://lucybain.com/blog/2014/null-undefined-undeclared/

Categories : JavaScript

Truth and Falsy in JavaScript

Apr 1, 2017

Javascript auto converts any value used in Boolean context (example an if condition) to either true or false. This way of implicitly converting a type is called as Implicit Type Coercion.

In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context.

A falsy value is a value that translates to false when evaluated in a Boolean context.

The following values are treated as false.

  • false
  • 0 (the number zero)
  • "" or '' (an empty string)
  • null
  • undefined
  • NaN (the result of failed mathematical operations)

All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).

The falsy values null and undefined are not equivalent to anything except themselves.

The falsy value NaN is not equivalent to anything — including NaN!

Use strict equal (===) and strict not equal (!==) in situations where truthy or falsy values could lead to logic errors.

We can test for several types of invalid data by simply passing a variable into an if expression.

if (value) {
  // value is truthy
}
else {
  // value is falsy
  // it could be false, 0, '', null, undefined or NaN
}

References

Categories : JavaScript