Substring means a portion of a string. Substring is extracted based on numeric indexes. This article will explain 3 different ways in which a substring can be retrieved from a string in javascript. Method 1: substr function Javascript substr function takes two arguments with their meanings given below. 1. Start index: Index of character from which the substring will be extracted. 2. Count: Total number of characters or length of substring. Syntax of substr function is substr(index, length) By default characters in a string start at index 0 from left to right. Below illustration explains the working of substr function in javascript. Following points shouldRead More →

It is often required to check the type of a value before performing some operation. Example, it would be better to check if the value of a variable is number before adding another number to it. This article will explain different ways in which you can check if a value is a number or not. number is a generic type in javascript. It means a value may be an integer(such as 10, 56, 1099 etc.) or a decimal(such as 10.99, 54.3232 etc.), it does not tell you the specific type of the value. Method 1: Using typeof operator Javascript typeof operator is followed by aRead More →

Reversing a string means creating a new string whose order of characters is the opposite. That is, last character is at first position, second last is at second and so on.There are many different ways in which this can be done in javascript and this article will explain 4 different methods with example. Method 1: Using for loopThis method reverses a string without using builtin reverse function and involves the following steps. Javascript program based on these steps is written below. Steps to runCopy the above code. Save it in a file with a .html or .htm extension. Right click the file and open itRead More →

Date comparison is a fundamental requirement for any application. Often when displaying data on a page, you need to display the latest record at the top and then the records that were created later. For that, the records should be compared based on their date of creation. Javascript provides different ways to compare dates using its Date object and this article will discuss them with examples. Method 1: Using relational operator comparison Two date objects can directly be compared using <, >, ==, <=, >= and != operators. Behind the scenes, their values are compared and the result is determined. Example, // create two dateRead More →

What is ajax AJAX stands for Asynchronous Javascript and XML and is used to fetch or send data from or to a server. AJAX is used for creating fast and dynamic web pages since it allows fetching data in the background after or during the page is loaded. In traditional applications, web pages were loaded completely. This means that entire page data will be fetched from the server and then rendered on the browser. If the user clicks a link, then the same would happen with the next page. But, with the use of AJAX, HTML, CSS and Javascript, now, complete page is not loaded.Read More →

A javascript object is a collection of properties in key-value pairs. An empty object means an object which does not contain any property. To know about javascript objects, refer this post. Before accessing a property of an object, it is recommended to check for object being empty to avoid run time errors. There are many methods to test empty object in javascript and this post will discuss them with examples. Method 1: Iterating over object properties Iterate over the properties of object which needs to be checked for being empty using a for loop. In every iteration, check if the property belongs to this objectRead More →

If some property with a given key is accessed in the object and it is not present, then an error is raised. Thus before accessing the property, it is recommended to check if a property with that key is present in the object so that if it is not present, then there is no error and an alternate action can be taken. This post will discuss different methods in which it can be checked if a key exists in an object. Method 1: Using in operator in operator checks for the presence of a property with the given key in an object and returns trueRead More →

A URL consists of a root and some parameters that carry some data. These parameters are in the key-value pairs and separated by ‘&’ character. Also, the list of parameters and the actual URL are separated by a ‘?’ character. This list of parameters is called URL parameters or query parameters while the complete string after the ‘?’ character is called query string. Example, https://www.google.com?enc=en&auth_user=u&lan=fr Here, actual or root URL: https://www.google.com query parameters: enc=en, auth_user=u and lan=fr query string: ?enc=en&auth_user=u&lan=fr Though you can get url query string using javascript by calling window.location.search but what if you want each url(or query) parameter with its value separately. This post will listRead More →

What is local storage Often you need to maintain a small amount of data for a web application such as a key, current timezone, user logged time etc. but you do not want to bear an overhead of sending this data to server or saving it in a database. Solution is to use local storage of the browser. What is local storage HTML5 introduced the concept of web storage where you can store data inside the browser. This data remains forever till it is not cleared even after the browser window is closed. Data in browser’s local storage is set and retrieved using javascript andRead More →