Javascript cookbook

Javascript – textarea and text input select all

Javascript can bse used to select full text in textarea and text put fields in HTML. Example – select all in textarea Example – 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

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

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 – 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 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 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

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

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

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

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 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 – 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

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 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

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 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 – 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 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 – 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 – 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 – 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 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 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 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 – 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

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

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