\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{yjcyun} \pdfinfo{ /Title (javascript.pdf) /Creator (Cheatography) /Author (yjcyun) /Subject (JavaScript Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{JavaScript Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{yjcyun} via \textcolor{DarkBackground}{\uline{cheatography.com/165804/cs/34715/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}yjcyun \\ \uline{cheatography.com/yjcyun} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 17th October, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Linking JS file}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Inline} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\textless{}head\textgreater{}\textless{}script\textgreater{}const role="developer"\textless{}/script\textgreater{}\textless{}/head\textgreater{}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Linking} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\textless{}body\textgreater{} \textless{}script src="script.js"\textgreater{}\textless{}/script\textgreater{} \textless{}/body\textgreater{}} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.89586 cm} x{4.08114 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Primitive Data Types}} \tn % Row 0 \SetRowColor{LightBackground} string & const role="dev" \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} number & const years=10 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{boolean} & const isTrue=true \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{undefined} & let children; \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} null & let empty=null \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} symbol & unique value \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} bigint & large integers that number type can't hold \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Truthy and Falsy}} \tn % Row 0 \SetRowColor{LightBackground} falsy values & 0, "", undefined, null, NaN \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} truthy values & everything else \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.54747 cm} x{4.42953 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{let vs. const vs. var}} \tn % Row 0 \SetRowColor{LightBackground} let & - can be reassigned\{\{nl\}\}- block scoped \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} const & - cannot be reassigned\{\{nl\}\}- block scoped\{\{nl\}\}- best practice \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} var & - hoisted(can be referenced before being declared)\{\{nl\}\}- can be redeclared\{\{nl\}\}- do not use in modern JS \tn % Row Count 8 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Conditionals}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// If else statement \newline if(age \textgreater{}= 18) \{ \newline status = "adult" \newline \} else \{ \newline status = "child" \newline \} \newline \newline // Switch statement \newline switch (day) \{ \newline case "monday": // if(day==="monday") \newline console.log("It's Monday") \newline break; // exits out of the switch statement \newline case "sunday": \newline case "saturday": // if(day==="saturday" || day ==="sunday") \newline console.log("It's the weekend") \newline break; \newline default: // else ... \newline console.log("Not a valid day") \newline \newline // Ternary Operator \newline const drink = age \textgreater{}= 18 ? "beer" : "bubble tea"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.4839 cm} x{1.4931 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Operators}} \tn % Row 0 \SetRowColor{LightBackground} add/subtract\{\{width=60\}\} & a + b - c \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} multiply/divide & a * b / 100 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} power: 2x2x2 & 2 ** 3 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} remainder: 13/5 remainder is 3 & 13 \% 5 \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} postfix increment/decrement: returns value before increment & a++, a-{}- \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{white} prefix increment/decrement: returns value after increment & ++a, -{}-a \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} find type of variable & typeof a \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} assignment & a = 'string' \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} a = a + b & a+=b (*=, /=, -=) \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} less and greater than & a \textless{} b, a \textgreater{} b \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} less or equal, greater or equal & a \textless{}= b, a \textgreater{}= b \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} logical and, or & a \&\& b, a || b \tn % Row Count 20 (+ 2) % Row 12 \SetRowColor{LightBackground} logical not & !(a === b) \tn % Row Count 21 (+ 1) % Row 13 \SetRowColor{white} strict equal, not equal: no type coercion & a === b, a !== b \tn % Row Count 23 (+ 2) % Row 14 \SetRowColor{LightBackground} loose equal, not equal: type coercion (do not use) & a == b, a != b \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.23505 cm} x{1.74195 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Type Conversion and Coercion}} \tn % Row 0 \SetRowColor{LightBackground} convert to number & \seqsplit{Number("2000")} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} convert to string & String(2000) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} convert to boolean & \seqsplit{Boolean("hello")} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} coerced to string: "a is 16" & "a is" + 16 \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} coerced to number: 80 & "100" - 10 - "10" \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} coerced to boolean: if(true) \{\} & if (hasMoney) \{\} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Objects}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{definition}}\{\{width=40\}\} & - a data structure where properties are stored in key-value pairs\{\{nl\}\}- order does not matter\{\{nl\}\}ex. `const student = \{ firstName: "Sheldon", lastName="Cooper", age: 10, fullName: () =\textgreater{} this.firstName + ' ' + this.lastName \}` \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} getting the value & `student.firstName` or \{\{nl\}\}`student{[}"firstName"{]}` \tn % Row Count 13 (+ 3) % Row 2 \SetRowColor{LightBackground} setting the value & `student.isSmart=true` \tn % Row Count 15 (+ 2) % Row 3 \SetRowColor{white} object methods & a function attached to an object \tn % Row Count 17 (+ 2) % Row 4 \SetRowColor{LightBackground} calling a method & `student.fullName()` \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Iteration}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{for Loop}} \newline // loops forward \newline for (let i = 1; i \textless{}= 10; i++) \{ \newline console.log(i) \newline \} \newline \newline // loops backward \newline for (let i = 10; i \textgreater{} 0; i-{}-) \{ \newline console.log(i) \newline \} \newline \newline {\bf{break statement: completely terminates the loop}} \newline for (let i = 1; i \textless{}= 10; i++) \{ \newline if(i === 3) break; \newline console.log(i); // 1, 2 \newline \} \newline \newline {\bf{continue statement: terminates the current iteration}} \newline for (let i = 1; i \textless{}= 10; i++) \{ \newline if(i === 3) continue; \newline console.log(i); // 1, 2, 4, 5, 6, 7, 8, 9, 10 \newline \} \newline \newline {\bf{while loop}} \newline let i = 1; // initialize counter \newline while(i \textless{}= 10) \{ // condition \newline console.log(i); \newline i++; // update counter \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{purpose}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- a block of code that performs certain tasks\{\{nl\}\}- useful for repeated tasks\{\{nl\}\}- either does something or returns value(s)} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{parameters}}: variable(s) functions accept} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`function fullName(first, last, ...params) \{\}`} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{arguments}}: values passed in when function is invoked} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`fullName("Sheldon", "Cooper", ...arguments)`} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{function declaration}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- define function with a function name\{\{nl\}\}- can be invoked before they are defined\{\{nl\}\}ex. `function foo(bar) \{\}`} \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{function expression}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- anonymous function stored in a variable\{\{nl\}\}- cannot be invoked before they are defined\{\{nl\}\}ex. `const foo = function (bar) \{\}`} \tn % Row Count 17 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{arrow functions}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- has no `this` keyword\{\{nl\}\}ex. `const foo = bar =\textgreater{} bar + a`} \tn % Row Count 20 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{default parameters}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- allow params to be initialized with default values if no value/`undefined` is passed\{\{nl\}\}ex. `function foo (bar = 0) \{\}`} \tn % Row Count 24 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{primitive type argument}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- function makes a copy of the original value} \tn % Row Count 26 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{reference type argument}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- functions makes a copy of the reference (the copy is still a value)\{\{nl\}\}- JS, arguments can only be passed by value} \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions (cont)}} \tn % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{first-class function}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}a concept in programming where functions are treated as first-class citizens meaning they can behave like variables} \tn % Row Count 4 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{higher-order function}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- a function that can be passed as an argument to other functions, or one that can be returned by another function or do both\{\{nl\}\}ex. \seqsplit{`bar.addEventListener('click'}, foo)`\{\{nl\}\}ex2. `const foo = () =\textgreater{} () =\textgreater{} console.log('hello')`} \tn % Row Count 10 (+ 6) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{`.call(object, arg1, arg2, ...)`}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- calls the function with a given `this` value and arguments provided individually\{\{nl\}\}- remember `this` is undefined on regular function call\{\{nl\}\}ex. `foo(num,str) \{return this.name + str\}`\{\{nl\}\}`foo(12, "hello") // 'this' keyword is undefined`\{\{nl\}\}`foo.call(barObject, 12, "hello")`} \tn % Row Count 18 (+ 8) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{`.apply(object, {[}arg1, arg2, ...{]})`}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- calls the function with a given `this` value, and arguments provided as an array\{\{nl\}\}ex. `const arguments = {[}12, "hello"{]}`\{\{nl\}\}`foo.apply(barObject, arguments)`\{\{nl\}\}`foo.call(barObject, ...arguments)`} \tn % Row Count 24 (+ 6) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{`.bind(object, arg?)`}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- returns a new function with `this` value bound to the provided object\{\{nl\}\}- arguments can be passed optionally to preset arguments\{\{nl\}\}ex. `const newFoo = foo.bind(barObject)`} \tn % Row Count 29 (+ 5) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{IIFE}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- immediately invoked function expression\{\{nl\}\}- useful if the function is only to be used once\{\{nl\}\}ex. `(function() \{console.log('hello')\})()`} \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions (cont)}} \tn % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{closure}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}- a function that remembers all the variables that existed at the function's birth place} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}