JavaScript Cheat Sheet: Arrays, Functions, Objects & More
Classified in Computers
Written on in
English with a size of 4.2 KB
JavaScript Cheat Sheet
JavaScript Arrays
var cars = ["Mercedes", "Tesla","Volvo"];
pop(): Removes the last element of an array.push(): Adds a new element at the end of an array.concat(): Joins various arrays into a single array.reverse(): Reverses the order of elements in an array.indexOf(): Returns the first index of a given element in an array.lastIndexOf(): Returns the last index of a given element in an array.join(): Combines elements of an array into a single string.sort(): Sorts array elements based on a condition.
Global Functions
These functions are built-in to every browser that runs JavaScript:
parseFloat(): Parses the argument and returns a floating-point number.parseInt(): Parses the argument and returns an integer.Number(): Returns a number converted from the argument.eval(): Evaluates JavaScript code represented as a string.
String Methods
toLowerCase(): Converts a string to lowercase.toUpperCase(): Converts a string to uppercase.charAt(): Returns the character at a specific index of a string.match(): Retrieves matches of a string against a provided pattern.indexOf(): Provides the index of the first occurrence of text in a string.lastIndexOf(): Provides the index of the last occurrence of text in a string.
Node Properties
attributes: Returns a live list of attributes of an element.childNodes: Returns a list of child nodes of an element.firstChild: Returns the first child node of an element.lastChild: Returns the last child node of an element.nextSibling: Returns the next node at the same level in the DOM tree.parentNode: Returns the parent node of an element.previousSibling: Returns the previous node at the same level in the DOM tree.
Node Methods
appendChild(): Adds a new child node as the last child of an element.getFeature(): Returns an object that implements the APIs of a feature.hasAttributes(): Returnstrueif an element has attributes, otherwisefalse.hasChildNodes(): Returnstrueif an element has child nodes, otherwisefalse.
JavaScript Data Transformation
map() Method:
var arr = [10,20,30]; var triple = arr.map(x => x * 3); triple; // [30,60,90]
filter() Method:
var arr = [13,40,47]; var odd = arr.filter(x => x % 2); odd; // [13,47]
JavaScript Date Objects
Date(): Returns a new date object with the current date and time.getDay(): Returns a number (0-6) representing the day of the week.getFullYear(): Returns the year as a four-digit number (yyyy).getHours(): Returns the current hour (0-23).
const d = new Date();
JavaScript Browser Objects
pageXOffset: Pixels scrolled horizontally from the left of the page.pageYOffset: Pixels scrolled vertically from the top of the page.document: Returns the document object of the window.screen: Returns the screen object of the window.screenLeft: Horizontal coordinate of the window relative to the screen.screenTop: Vertical coordinate of the window relative to the screen.top: Returns the topmost browser window.screenX: Same asscreenLeft, but required by some browsers.screenY: Same asscreenTop, but required by some browsers.