Git Product home page Git Product logo

javascript-reference's Introduction

Javascript Reference

typeof null is object

The type of null is not null as expected. It's object.

// www.justjavascript.com
typeof null === 'object'; // true

Add element at the beginning (left) of the array using the spread operator.

const arr = [1, 2, 3];
return [0, ...arr]; // [0, 1, 2, 3]

Add element at the beginning (left) of the array using unshift.

const arr = [1, 2, 3];
arr.unshift(0); // returns new length: 4
return arr; // [0, 1, 2, 3]

Add element at end (right) of the array using the spread operator.

const arr = [1, 2, 3];
return [...arr, 4]; // [1, 2, 3, 4]

Add element at the end (right) of the array using push.

const arr = [1, 2, 3];
arr.push(4); // returns new length: 4
return arr; // [1, 2, 3, 4]

Capitalize first letter

// https://stackoverflow.com/a/1026087/1013
function capitalizeFirstLetter(string) {
	return string.charAt(0).toUpperCase() + string.slice(1);
}

Get random item from array

// https://stackoverflow.com/a/5915122/1013
function rand(items) {
	return items[Math.floor(Math.random() * items.length)];
}

Falsy values

There are only 8 falsy values. The others are truthy.

// https://developer.mozilla.org/en-US/docs/Glossary/Falsy
function f() {
	// prettier-ignore
	return [
      Boolean(false),
      Boolean(''),
      Boolean(``),
      Boolean(""),
      Boolean(0),
      Boolean(0n),
      Boolean(-0),
      Boolean(-0n),
      Boolean(NaN),
      Boolean(null),
      Boolean(undefined)]
}
f(); // returns array of `false`s

Unexpected Truthy Values

// https://developer.mozilla.org/en-US/docs/Glossary/Falsy
function f() {
	return [Boolean(new Boolean(false)), Boolean('false'), Boolean({}), Boolean([]), Boolean(-1)];
}
f(); // [true, true, true, true, true]

Double bang is the same as Boolean

Double bang is like not not something. Todo: check if it's exactly the same.

// It's basically !(!0) that is converted to !(true) which in turn becomes false
!!0; // false

Right associative makes a global variable

// https://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript/1758912#1758912
function f() {
	//prettier-ignore
	var a1 = a2 = 1 // this is like var a1 = (a2 = 1)
	//prettier-ignore
	var b1 = 1, b2 = 1
	return [window.a1, window.a2, window.b1, window.b2];
}

f(); // [undefined, 1, undefined, undefined]

Main types

// 2020-5-8
// bigint at 70% https://caniuse.com/#feat=bigint
// symbol 93% https://caniuse.com/#feat=mdn-javascript_builtins_symbol

typeof undefined; // "undefined"
typeof null; // "object"
typeof true; // "boolean"
typeof 1; // "number"
typeof 1.1; // "number"
typeof 2n; // "bigint"
typeof 'some text'; // "string
typeof Symbol('some value'); // "symbol"
typeof function g() {}; // "function"
typeof {}; // "object"
typeof []; // "object"
typeof new Date(); // "object"


typeof Boolean // "function"

Double typeof

typeof typeof 10; // "string" (typeof returns string "number")

Promises vs setTimeout

// What the heck is the event loop https://www.youtube.com/watch?v=8aGhZQkoFbQ
// Jaffa Cake Asia: https://www.youtube.com/watch?v=cCOL7MC4Pl0
// Sync BEFORE promises (microtask) BEFORE settimeout (macrotask)
const result = [];
function f() {
	setTimeout(() => {
		result.push('settimeout');
	}, 0);
	Promise.resolve().then(() => result.push('promise'));
	Promise.resolve().then(result.push('this is not a function, so it executes sync'));
	result.push('sync');
}
f();
setTimeout(() => {
	console.log(result);
}, 1);

bigints have comparison gotchas

// https://javascript.info/bigint

2n > 1; // true
1n > 1; // false
1n == 1; // true
1n === 1; // false
1n < 1; // false

Automatic semicolon insertion

//  ASI https://stackoverflow.com/a/2846298/1013
function f() {
	return {
		a: 'hello',
	};
}

function g() {
	return;
	{
		a: 'hello';
	}
}

f(); // {a: "hello"}
g(); // undefined

Infinity

//
function f() {
	return 2 / 0 === Infinity;
}
f(); // true

Not a Number

Number.isNaN(0 / 0); // true
Number.isNaN('hey' / 0); // true
Number.isNaN('hey'); // false

Floating point math error

// https://0.30000000000000004.com/
// For some reason JS is famous for this, but there are many languages
// that do the same.
// A way to think of it is it's like 1/3 is 0.3333(3) on base10.
// In base2 this thing happens for 0.1 + 0.2.

0.1 + 0.2 == 0.3; // false
0.1 + 0.2 === 0.3; // false
0.1 + 0.2; // 0.30000000000000004

Palindrome in less than 160 characters

// The same word if written back and forth.
// https://www.toptal.com/javascript/interview-questions
function f(str) {
	return str === str.split('').reverse().join('');
}

f('aba'); // true
f('aab'); // false

React

Forgetting to return a function

// inspiration https://twitter.com/donavon/status/1308468196559773696?s=12

useEffect(() => {
	const timerID = setTimeout(() => {
		// do stuff
	}, 2000);
	return clearTimeout(timerID);
}, []);

Notes

mdjs and mdjsf to add snippets on vscode. Don't forget the ctrl + space!

javascript-reference's People

Contributors

arturcarvalho avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.