├── .gitignore ├── .prettierrc ├── .eslintrc.json ├── README.md ├── package.json ├── NaN.js └── NegativeZero.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["airbnb", "prettier"], 3 | "plugins": ["prettier"], 4 | "rules": { 5 | "prettier/prettier": ["error"] 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # javascript-bits 2 | 3 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javascript-bits", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "eslint" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "eslint": "^6.1.0", 14 | "eslint-config-airbnb": "^17.1.1", 15 | "eslint-config-prettier": "^6.0.0", 16 | "eslint-plugin-prettier": "^3.1.0", 17 | "prettier": "^1.18.2" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /NaN.js: -------------------------------------------------------------------------------- 1 | // 1. NaN 2 | 3 | const a = Number('n/a'); 4 | 5 | const b = 'a' - 'b'; 6 | console.log(a, b); // NaN NaN 7 | 8 | // 1.1 The **ONLY** datatype which is not equal to itself 9 | 10 | console.log(a === a); // false 11 | console.log(a == a); // false 12 | 13 | console.log(undefined === undefined); // true 14 | console.log(null === null); // true 15 | console.log(NaN === NaN); // false 16 | 17 | // 1.2 Ways to find NaN (ES6) 18 | 19 | // 1.2.1 - Older ways of finding NaN 20 | 21 | console.log(isNaN(b)); // true 22 | 23 | /* global isNaN checks whether the value is a valid Number or not. 24 | 25 | The way it works is: 26 | 1. First it coerce to Number 27 | 2. Then it checks, whether the passed value results in NaN 28 | 29 | In below example, "Hello Earth" String is coerced to Number which results in NaN. 30 | Hence it is true. 31 | 32 | TL;DR: global isNaN checks for NaN after the input being coerced to number! 33 | */ 34 | console.log(isNaN('Hello Earth!')); // true 35 | 36 | // 1.2.2 - ES6 way of checking NaN 37 | 38 | console.log(Number.isNaN(a)); // true 39 | 40 | // 1.2.3 - Check two NaN 41 | 42 | console.log(Object.is(b, a)); // true 43 | 44 | // 1.2.4 - NaN check in ES6 way 45 | 46 | console.log(Object.is('Hello Mercury!', NaN)); // false 47 | 48 | console.log(Number.isNaN('Hello Sun!')); // false 49 | -------------------------------------------------------------------------------- /NegativeZero.js: -------------------------------------------------------------------------------- 1 | // 2. Negative Zero (-0) 2 | 3 | const a = -0; 4 | 5 | // Negative Zero equals itself 6 | console.log(a === -0); // true 7 | 8 | // 2.1 9 | // Irrespective of the sign, Negative equals to 0 10 | console.log(a === 0); // true 11 | 12 | // 2.2 13 | // It is neither greater than nor less than that of 0 14 | console.log(a < 0); // false 15 | console.log(a > 0); // false 16 | 17 | // 2.3 18 | // Ways to find -0 19 | // Object.is can determine whether the given value is -0 or not 20 | // Object.is results in false when -0 is checked with 0 21 | 22 | console.log(Object.is(a, -0)); // true 23 | console.log(Object.is(a, 0)); // false 24 | 25 | // 2.4 26 | // Math.sign helps to figure out the sign of the given number, results in 1 and -1 for positive and negative values respectively 27 | console.log(Math.sign(-5)); // -1 28 | 29 | // 2.4.1 30 | // when it comes to 0 and -0, Math.sign works differently 31 | console.log(Math.sign(0)); // 0 32 | console.log(Math.sign(-0)); // -0 33 | 34 | // 2.4.2 35 | // helper function to get sign 36 | const getSign = value => { 37 | return value !== 0 ? Math.sign(value) : Object.is(value, -0) ? -1 : 1; 38 | }; 39 | 40 | console.log(getSign(0)); // 1 41 | console.log(getSign(-0)); // -1 42 | console.log(getSign(-5)); // -1 43 | 44 | // 2.4.3 45 | // Infinity 46 | // (Number / 0) results in Infinity 47 | // (Number / -0) results in -Infinity 48 | 49 | console.log(1 / 0); // Infinity 50 | console.log(1 / -0); // -Infinity 51 | 52 | // This is one of the best solution to find -0 ( to impress your friends 😜 ) 53 | 54 | const isNegativeZero = value => { 55 | return value === 0 && 1 / value === -Infinity; 56 | }; 57 | 58 | console.log(isNegativeZero(-0)); // true 59 | console.log(isNegativeZero(0)); // false 60 | console.log(isNegativeZero(-5)); // false 61 | 62 | // 2.5 63 | // As like 0, -0 also coerce to false in terms of Boolean operation 64 | console.log(Boolean(0)); // false 65 | console.log(Boolean(-0)); // false 66 | --------------------------------------------------------------------------------