add event listener
Adds an event listener to an object. Once the event in triggered function passed as the second argument is fired.
copy
Adds an event listener to an object. Once the event in triggered function passed as the second argument is fired.
copy
Example of looping through an array of objects and adding click event listeners.
copy
ES6 arrow function syntax. In one line arrow functions the function output is automatically returned.
copy
Step by step explanation of the ES6 arrow function syntax.
copy
Async attribute can be added to the script tag loading an external file. Script will be loaded asynchronously and executed in the background. Note, this can cause script executing before page has been completely parsed so make sure you account for that in your code.
copy
Example of the ES6 class declaration.
copy
Append a style class to the selected element. Use only the class name without the dot notation. 'add' method belongs to the 'classList' property.
copy
Check if the given class exists in the element's class list. Returns True if the class exists. Use only the class name without the dot notation. 'contains' method belongs to the 'classList' property.
copy
This method lets you return a class name assigned to a given index value in the class list. Useful for looping through the 'classList' items. 'item' method belongs to the 'classList' property.
copy
Use this method to remove one or more classes from the class list. 'remove' method belongs to the 'classList' property.
copy
This method lets you toggle a class in the class list property. It will also return True if the class has been added and False when the class was toggled off. Use only the class name without the dot notation. 'toggle' method belongs to the 'classList' property.
copy
Closures are functions using externally declared variables in their own scope. Useful when debugging Closures - console.dir method allows you to preview a function scope.
copy
Console assert takes two attributes. If the first attribute is evaluated as false it will log a message passed as the second attribute.
copy
Prints a detailed view of DOM JS object to the console.
copy
Returns value to the console. "\n" will start a new line
copy
Outputs an object to console in format of a table.
copy
Create and append HTML element. In the example an empty paragraph is being created using createElement property. Then the content is added through innerHTML property and finally the new element is appended to the body.
copy
A simple example of a time counter. Counts down days, hours, minutes and seconds to a set date.
copy
Defer attribute can be added to the script tag loading an external file. This will defer execution of the file until the whole page is parsed. It's a common technique improving site performance by delaying potentially blocking assets.
copy
Using setTimeout function this loop example lets you delay the iteration.
copy
To delete a cookie pass the name and a date set in the past.
copy
Use it to extract specific data from an object or an array.
copy
It allows you to pass function arguments as an object. You can set default values for the arguments (see "size" argument) and it also allows for omitting arguments when calling a function rather than using null.
copy
Use object destructuring to pass arguments in the function parameters.
copy
Copies referenced object to the clipboard.
copy
Document location gives you an access to an object containing useful information about the current URL and methods for updating and redirecting. Use the dot notation to access object properties and methods.
copy
Gets the referrer url of the page.
copy
You can use 'client' and 'offset' variants of width and height properties to get the dimensions of elements. The 'clientWidth' and 'clientHeight' properties capture element sizes including padding only. The 'offsetWidth' and 'offsetHeight' properties capture the full size of the element including padding, margin and border.
copy
This ES6 feature allows you to easily import and export whole modules or parts of them between javascript files.
copy
Also referred to as template strings. Allows for a simpler use of variables within blocks of text and multi lines.
copy
Extend an existing ES6 class. super() references the parent class constructor.
copy
Filter out values from a set of data.
copy
Finds and selects the closest parent element to the selected one. It will check all elements all the way to the document root.
copy
Loop though the code number of times defined in the "for" block.
copy
Similar to for loop but you don't need to specify the number of iterations. The loop uses an iterator function built in to arrays.
copy
Loops through an array of items.
copy
Get a value of the selected data attribute.
copy
Read the cookie value by assigning it to a variable. This will return a string containing all the name value pairs set in a cookie.
copy
Use dataset property to return an object containing all data attributes set on the element. You can also get value of a particular data attribute by using dot notation and referencing the unique name part of the attribute.
copy
Get the scroll position from the top of the window.
copy
Use this helper function to check if the element has a given class. It uses a regular expression to match the queried class name. First attribute of the helper function takes the element name. Pass the name of the class you want to check as the second attribute.
copy
Inner HTML lets you get and set the content of selected element.
copy
Inner text lets you get and set the text of selected element.
copy
Select last n items from array
copy
Used for storing key value pairs and can be accessed through the window object.
copy
Remove an item from localStorage object.
copy
Performs an operation on each mapped value.
copy
Math.floor() method takes a fraction as an argument and returns the largest integer equal or smaller than the original value.
copy
Returns the largest number from the supplied values.
copy
Math.round() method takes a single number as an argument and returns the closest integer.
copy
You can capture X and Y mouse position using clientY and clientX properties.
copy
Navigation timing is part of the Performance API that lets you benchmark web applications. It allows to measure various points of page load, including the time it takes for the server to respond. In the attached example we are capturing total page load time, DOM start, DOM interactive, content loaded, DOM complete, and total page render time. These are just a few examples form the API. For the full explanation visit https://www.w3.org/TR/navigation-timing/
copy
Use destructuring to pass variables to an object.
copy
Object literal pattern, also known as modular pattern, is a way of organizing your javascript code. The biggest advantages of this approach is modularity, encapsulation and a clear, readable structure.
copy
Add event listeners for checking the network status. Capturing those events lets you handle offline and online status of the application.
copy
Runs a function when an object is clicked.
copy
Gets domain name of the page.
copy
Location is a property of the Window object and it allows to set a new page url effectively acting as a page redirect.
copy
Converts a string to an integer.
copy
The Promise object allows you to make an asynchronous call to another function. The 'resolve' function is called when the asynchronous call is successful otherwise it falls back to 'reject' function.
copy
Use push method to append a new item to the end of an array.
copy
Returns the first descendant of the queried element.
copy
Returns a list of nodes that are descendants of the queried element. To operate on the returned values you can loop through it using forEach() or convert the list to an array using Array.from()
copy
This function removes selected element from DOM.
copy
Request animation frame lets you line-up changes for the next window paint.
copy
Return statement finishes function execution and can output a value.
copy
Selects body element.
copy
Select all document elements with a given data attribute.
copy
Selects the next sibling of the element. Returns null if no element is found.
copy
Selects the next sibling of the element including text. Returns null if no element is found.
copy
Selects the parent of the element. Returns null if no parent is found.
copy
Selects the previous sibling of the element.
copy
Self invoking function lets you immediately execute code. Another benefit is scoping, so you are not risking cluttering the global object. It's also a good practice to check for the window object and other dependencies by passing them as arguments.
copy
Set the value of the targeted data attribute.
copy
Create cookie by passing a cookie name, value, expiry date and path to witch the cookie belongs.
copy
Set a cookie with one year expiry date.
copy
Allows to run a function in regular intervals. The second argument takes the interval time in milliseconds. The example checks for the existence of the class before stopping the loop.
copy
Assigning array to Float64Array array type lets you correctly sort its numeric values.
copy
Splice method lets you add and remove items from an array. I takes a minimum of two arguments, first one marks the index of the array item, second, is a number of items to be removes, set to 0 if no items are to be deleted. The following arguments are the new items to be added to the array. When taking out items from the array, splice will automatically return them.
copy
Check if the string ends with the given text.
copy
Check if the string includes the given text.
copy
Check if the string starts with the given text.
copy
Runs through a set of cases and executes the code when the condition is met.
copy
Ternary operator evaluates first parameter followed by two values. First value is returned when the condition is true otherwise the second value is returned.
copy
toFixed() method takes a single integer as an argument determining how many decimal numbers should the original value be fixed to.
copy
Try catch allows to run a block of code and capture any exceptions.
copy
Returns user agent header identifying browser type, name and version.
copy
Es6 introduced 'let' and 'const' to register variables. By default use 'let' to indicate that the variable can mutate or be reassigned, otherwise use 'const'. The use of 'var' has been limited to use in the global scope.
copy
Execute the code inside the loop as long as the statement is true.
copy
Window on load property will run the handler function when the load event is fired. The event is raised when the document, including all objects and elements, has fully loaded.
copy
You can use 'inner' and 'outer' variants of width and height properties to get the dimensions of the window element. The 'innerWidth' and 'innerHeight' properties capture window sizes excluding scrollbars or toolbars. On the other hand 'outerWidth' and 'outerHeight' properties capture the full area of the browser window including all the panels and scrollbars.
copy