Javascript – check if string is number
The isNaN() function determines whether a value is NaN (Not-A-Number) or not. Note that parseInt approach may not work with strings having valid number prefix. read more
Javascript – parseInt
The parseInt() function parses a string argument and returns an integer of the specified radix (default 10). In case the string starts with a valid read more
Javascript – string ends with check
ECMAScript 6 String.prototype.endsWith() method can be used to check if a string ends with a suffix. But it is not yet supported by all browsers. read more
Javascript – string starts with check
ECMAScript 6 String.prototype.startsWith() method can be used to check if a string starts with something. But it is not yet supported by all browsers. Another read more
Javascript array forEach
Few notes on Javascript array forEach Array arr is passed to function as local variable a Array arr is also available in function here as read more
Javascript – append item to array
Javascript code to append one or multiple values at the end of array Append one value Append multiple values
Javascript – remove last item from array
Javascript code to remove last item from array using arr.pop() Append one value
Javascript – prepend item to array
Javascript code to prepend an item at the beginning of array using arr.unshift() Prepend one value to array
Javascript – remove first item from array
Javascript code to remove first item from array using arr.shift() Remove first item from array
Javascript – create array from 0 to N-1 in nodejs
In Javascript to create an array of number from 0 to n-1, we can use spread operator. It is part of ECMAScript (version 6). Currently read more
Javascript convert array to json string
JSON.stringify() can be used to convert a given javascript value to json strng. Usage: JSON.stringify(value[, replacer[, space]]) Here are some examples Example – JSON.stringify() default read more
Javascript for-in loop to iterate over keys of javascript object
for in loop over javascript associative array for in loop over javascript DOM object
Javascript – DOM appendChild example
Javascript DOM method appendChild can be used to append a child to existing DOM element. For example it can be used to append an Image, read more
Javascript – get computed style of an element
To get computed style of an element the following approaches can be taken: Vanilla javascript: window.getComputedStyle(element) jQuery: .css(propName) Example – computed style using Javascript Example read more
Javascript – how to view text inside DOM element
Javascript DOM method textContent can be used to get the text content inside a DOM. This can be useful to obtain content from a web read more
Javascript – img onload examples
Sometime we need to call a function after an image has been loaded. We can attach onload even handler on image either in html itself read more
Javascript – ready vs on load event handler example
HTML document ready event is called before image assets, etc. are loaded on the page. window onload handler is called once all assets have been read more
Javascript – use querySelector to set dom element html
Javascript querySelector can be used to select and set html of a dom element using css selectors. This can be done with jQuery also. But read more
Javascript DOMContentLoaded listener example
DOMContentLoaded event is fired when document DOM is ready and before images, etc. are loaded. It is similar to jQuery $(document).ready(). Here is code snippet read more
Javascript event bubble vs capture
Javascript/HTML events can propagate in two ways in DOM API. This is relevant when more than one element are listening on same event. Event propagation read more
Javascript how to stop event propagation
Javascript/HTML event can be handled by more than one dom elements and after one handler, it propagate to next element. API Event.stopPropagation() can be used read more
Javascript onscroll event handler
Javascript onscroll event handler can be attach to any DOM element. Attaching it to document is a typical use case. But it can be attached read more
Javascript onscroll event handler with throttling
Javascript onscroll event handler is called on every onscroll event. At times we want to ensure that it is not called again before a certain read more
Make an element draggable using Vanilla Javascript
Javascript event handler mousedown and mousemove can be used to make an element draggable without using any other library. This can be made to work read more
Multiple onload handlers using vanilla Javascipt
Attaching a function to windows on load event can be done in multiple ways using plain vanilla Javascript. One option is to use wondow.onload where read more
Use universal selector to get all DOM nodes in vanilla Javascript
The universal selector (*), matches the name of any element type. It matches any single element in the document tree. Some ways it can be read more
document querySelector examples
Javascript querySelector() and querySelectorAll() can be used to get DOM element based on given CSS selector. These works on document and element objects both. querySelector() read more
Javascript – dump all handlers on window object
DOM window object has many javascript event handlers attached to it. It may be handy to dump and view all of these. Here is code read more
Are Javascript functions objects?
A Javascript function is also an object of type function. So it can be used as an object and can have member variables. Also note read more
Javascript – declare and invoke anonymous function at the same time
Javascript can declare anonymous function and call it at the same time using the following syntax: (function (arg1, arg2, …) { }) (arg1, arg2, …); read more
HTML5 drag and drop – move an element to dropzone
HTML5 Drag and Drop interfaces enable applications to use drag and drop (using mouse) features in web browser. It involves dragging an element and dropping read more
Using JSLint on command line on Ubuntu linux – quick start guide
Sometimes javascript is developed and tested by pushing it to the website and then checking if it is working in browser as intended. If your read more
Javascript – call vs apply
Javascript call() and apply() can be used to invoke a function. They are mostly same. The only difference is in apply arguments are passed as read more
Javascript – implement class using function
Javascript classes can be created using function. Here a function can be instantiated using new keyword. The member variables and methods in function can be read more
Javascript – iterate over function arguments
Sometimes it is useful to iterate over function arguments programmatically. Function arguments can be access using arguments variable. Here is code snippet for this. Note read more
Javascript – print all methods of an object
To print all methods of an object, we can use Object.getOwnPropertyNames and check if the corresponding value is a function. The Object.getOwnPropertyNames() method returns an read more
Javascript – run a function at regular interval
Javascript setInterval can be used to make repeated calls to a function or code with fixed interval. If there is a possibility that function logic read more
Javascript – textarea and text input select all
Javascript object.select() can bse used to select full text in textarea and text put fields in HTML. Example – select all in textarea Example – read more
Javascript arrow function examples
Arrow (=>) function expressions are shorter syntax compared to function expressions. Arrow functions are always anonymous. Here are some commonly used usage syntax patterns. singleParam read more
Javascript check if variable is defined
Code snippet to check if a given javascript variable is defined. This also covers a variable having null value and non existing entry in an read more
Javascript local and global variables
Javascript variables can be local or global scoped depending upon how the are declared. In HTML all global scoped variables becomes part of window object. read more
Javascript parse json string
JSON.parse() can be used to parse a given json string into javascript value (array, string, etc.) Here are some examples Example – parse json containing read more
Javascript prototype examples
Javascript function can be used to define classes. Inside classes, member properties (containing functions, objects, etc.) can be defined either as member variables in constructor read more
Javascript settimeout example
Javascript setTimeout can be used to scheduled a function or code to run at a specified delay. This is pretty powerful API and can be read more
Javascript sleep implementation
Javascript does not have any sleep function. At times (e.g. to simulate a specific situation) we may need to sleep or delay the code for read more
Requirejs – quickstart guide for beginners
RequireJS is a JavaScript file and module loader. It can be used in browsers and other javascript environments like Node. This article is intended as read more
Javascript – catch errors using window.onerror
Javascript errors can be caught using window.onerror event handler. Whenever a javascript error occurs browser calls this function. In this tutorial we’ll write error handler read more
How to print javascript object to log
Often we need to print a javascript object to console log or somewhere else for debugging purpose. These are some handy ways to dump javascript read more
Javascript – Number function
The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. It read more