Currently browsing Methods, Events and Scopes
Simplifying Conditional Statements in JavaScript with the && Operator
JavaScript provides several ways to handle conditional logic. One powerful method is the logical AND (&&) operator. This tutorial will…
A collection of modern native JavaScript object and array utilities
Arrays Chunk Creates an array of elements split into groups the length of size. Compact Creates an array with all…
Code Golfing Tips & Tricks: How to Minify your JavaScript Code
JavaScript golfing is the process of writing the smallest amount of JavaScript code to do something awesome.
The Complete JavaScript Strings Reference
JavaScript strings are deceptively complex constructs. There are actually two different types of strings – string Literals and string Objects…
JavaScript Form Validation
This JavaScript code snippet adds inline validation to any form field (input, select and textarea). Feel free to customise it…
How to Optimize a Theoretical Funnel Page Load Speed
Here’s a quick snippet to preload the next step in a theoretical funnel/workflow: PHP JavaScript
Numerical Integration Using JavaScript
This is a quick reminder on how to use mathematical functions inside JavaScript. Instructions Trig: sin, cos, tan, cot, sec,…
Random JavaScript Tutorials
I needed some structural updates for a site, and among them, I added some JavaScript snippets. I will show you…
How to Declare a Global Variable inside a JavaScript Function
A JavaScript global variable is a variable with global scope, meaning that it is visible and accessible throughout the program, unless…
How to execute code for X visits
This JavaScript will let you execute code a certain number of times. For example, you might want to only show…
JavaScript shortcuts for TRUE and FALSE
Here’s a neat trick if you’re into micro-optimization. JavaScript compressors and minifiers are used to make JavaScript code smaller and,…
How to Expire localStorage Items and Replace Cookies
I don’t use 🍪 cookies any more, I think they are too… rigid. I use localStorage items. By default, they…
How to extract a value from an element and, optionally, round it
This JavaScript snippet will extract a price from a DOM element, remove all alphanumeric characters, such as currency or symbols…
How to create a YouTube video cover using the YouTube Iframe API
There’s an increasing trend of adding full-width (possibly full-height) YouTube video covers with autoplay. Using the YouTube Iframe API, this…
How to Get and Set URL Parameters with JavaScript
This is an old script, which helped a lot with the initial phase launching a project on all browsers and…
How to Implement a Service Worker in Your Website and How to Create a Basic PWA
You definitely don’t need a plugin for this. Although there are plugins which help with creating a service worker and…
Cross-browser Query Parameter Getter and Setter
Here’s a cross-browser compatible way of getting query parameter from a URL or setting query parameters and reloading the page. An…
How to Autoplay a Video Element Using JavaScript
The latest Chrome version has stopped video autoplay if the video is not muted. Firefox and the other browsers will soon…
JavaScript Data Types
Every variable in JavaScript has a data type which dictates the values that can be stored in it. However, JavaScript…
How to Compare Version Numbers in JavaScript
The basic idea to make this comparison would be to get arrays of parts from the version numbers, and then compare pairs…
Useful JavaScript String Prototypes
This is a function to remove all double whitespaces and preceding line breaks. This is a function to replace all line…
truthy and falsy in JavaScript
In JavaScript, any variable can be used in a logical context, such as the condition of an if statement or…
Promises in JavaScript
Writing code in an async programming framework like node.js becomes super complex and unbearably ugly, superfast. You are dealing with…
Custom Events in JavaScript MVC
Events are the bread and butter in UI development. They are frequently used to kick off JavaScript to deal with…
Cutting Loose: Dynamic Namespacing in JavaScript
Most of the JavaScript libraries come wrapped in an easy to refer single object. The object acts as a namespace,…
Understanding JavaScript Currying
I recently fully understood the concept of utilizing Currying in JavaScript. The core concept can be grasped from these references: Wikipedia entry on…
John Resig’s Inheritance
John Resig, the creator of jQuery, has created this JavaScript inheritance code. I am still using it now to simplify several…
Encapsulation: Private Attribute
In JavaScript OOP there are attributes called private attributes. Knowing if an attribute is private or public, an API user…
Encapsulation: Private and Public Methods
When it comes to developing APIs, self-documenting code is very important, as it simplifies the user’s effort in filtering the…
Giving Object Context to Function Calls
JavaScript has a dynamic object named this. By default, this equals to the global object container, so if we have…
Use ‘this’ Keyword with setTimeout()
setTimeout() is a great help to many JavaScript developers. For example, you may wish to add a delay to allow…
JavaScript String Multiline Trim/Cleanup
This small JavaScript prototypes enhance the trim() function and allow for better trimming and/or replacing line breaks with commas (or…
How to Open a JavaScript Centered Popup Window
This tutorial is about JavaScript popups and how to show a window in the center of the screen and focus on it.…