What does not not operator do (!!) ?

Mar 16, 2020

It converts Object to boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.

References:

  • https://dev.to/macmacky/70-javascript-interview-questions-5gfi#16-what-does-the-operator-do
  • https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript
Categories : JavaScript

Difference between function declaration and function expression

Mar 16, 2020

var result = add(10, 20);
console.log(result); // 30

// Function declaration
function add(num1, num2) {
	return num1 + num2;
}
// Function expression
var add = function (num1, num2) {
	return num1 + num2;
};

Function declarations are hoisted to the top of the code by the browser before any code is executed. Specifically, all of the functions written with function declarations are “known” before any code is run. This allows you to call a function before you declare.

Function expressions, however, do not hoist. If you try to run a function before you’ve expressed it, you’ll get an error.

References

Categories : JavaScript

Difference between Pure and Function React JS component

Mar 16, 2020

React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

Function React component is that which accepts a single “props” (which stands for properties) object argument with data and returns a React element.

References:

  • https://reactjs.org/docs/react-api.html#reactpurecomponent
  • https://reactjs.org/docs/components-and-props.html
Categories : JavaScript

What are Curry functions in JavaScript

Jul 10, 2019

Currying is a technique of evaluating function with multiple arguments, into sequence of function with single argument. That is, when we turn a function call add(1,2,3) into curriedAdd(1)(2)(3) .

function curriedAdd(x) {
    return function(y) {
        return function(z) {
            return x + y + z;
        };
    }
}

curriedAdd(1)(2)(3)  // result would be 6

let curriedAdd_1_2 = curriedAdd(1)(2);
curriedAdd_1_2(3) // result would be 6
curriedAdd_1_2(10) // result would be 13

References

Categories : JavaScript

Which one to choose Mocha or Jest

Jul 10, 2019

If you have a large project with the need for flexibility and customization then Mocha is probably the choice for you. If you have a smaller project and don’t need the extra setup and configuration up front, Jest is probably the better option.

References

  • https://blog.usejournal.com/jest-vs-mocha-whats-the-difference-235df75ffdf3
  • https://medium.com/airbnb-engineering/unlocking-test-performance-migrating-from-mocha-to-jest-2796c508ec50
Categories : JavaScript