Many times you want to load a web page from your application. You can create a hyper link but you might want to change the page on click of a button or some other event. In such cases, javascript’s page redirection can be used. There are following three ways in which you can load a new URL or redirect to another page using javascript. Method 1: Using window.location.href property Javascript’s window.location object has an href property which represents the URL of current page. Assign this property to the desired URL and it will load the supplied URL in the current page. Example, window.location.href = ‘https://google.com’;Read More →

String comparison is a necessary operation required in every application and is used to determine if two string values are equal to one another or not. Javascript provides provisions to compare two strings with each other to check if they are equal or not. This article will list down different ways in which two strings can be compared in javascript. Method 1: Using equality operator Javascript’s equality operator can be used to compare 2 strings. Equality operator consists of double equal to sign(==) with strings to be compared on its either sides. It will return true if the strings are equal or same and falseRead More →

Sometimes on a button click or any other event, you need to get the current URL of the browser window. Javascript provides a simple method to determine the URL. window object Javascript’s window object has a property location which contains information about the current document or the window in which the script is running. This object can be used for getting the URL and host of the current page as below. window.location.href :           Will return the complete URL of the current page. window.location.host:            Will return the host and port from the URL of the current page.Read More →

What is a prime number? A prime number is a number that is completely divisible by only 1 and itself. By completely divisible means that when divided by a number, the remainder is 0. 0 is not a prime number. In other words, a prime number is a number that has only 2 factors, 1 and the number itself. Examples of prime numbers are 2, 3, 7, 11, 13, 17, 19, 23, 29, 34 and so on. Find all prime numbers within a limit This post will show you how to create a javascript program that determines all the prime numbers less than a givenRead More →

Cloning an object means creating its exact copy. Cloned object would have the same properties and methods as the source object. There are different ways of creating a clone of an object in javascript. Method 1: Iterating over source object Iterate over the object which needs to be cloned using for–in loop and assign the key value in each iteration to the target object. Example, // source object let car = { color: ‘red’, brand: ‘BMW’, print: function() { console.log(‘Car object’); } }; // declare the target object const clone = {}; // iterate over properties of source object for(let key in car) { //Read More →

Assume you have a parent element with multiple child elements. On some event such as a button click, you want to hide all the children of this element at once using jQuery. Suppose the page structure is as given below. <!– parent element –> <div id=”parent”>      <!– Child elements –>      <div>Child One</div>      <div>Child Two</div>      <div>Child Three</div> </div> <button onclick=”hideChildren();”>Hide</button> Above HTML code contains a div with id parent with three child div elements. It also contains a button which when clicked shall hide all the three child elements. Note that button click calls a hideChildren function whichRead More →

Scenario Many times you want to perform some task repeatedly after a definite time interval or call a particular function after every fixed time elapse. Example, checking for the status of completion of a process and showing the result when the process completes or displaying some message on the browser after every few seconds etc. Javascript way In javascript a task can be repeated using setTimeout method. This method takes 2 arguments, first one is the task which needs to be done. This task is provided in the form of a function. Second argument is the time after which the task will be repeated. TimeRead More →

Overview Converting a string to number is a much required feature in any programming language. Number in javascript can be inferred as an integer(such as 10, 99, 1111 etc.) or a decimal number(such as 5.67, 9.999 etc.), also called floating numbers. Scenario Suppose you have a json string coming from a web server to browser. This json string has a field named visitorCount and you want to increment 1 to this value every time the page loads. Now, if you straightaway get its value and add 1 to it, it will be appended to the value instead of adding 1 to it. Thus an initialRead More →

    What is an Object? An object is an entity which has some attributes(or fields or properties) and functions. Attributes represent state of an object and functions represent the task that can be done by the object. Attributes are key-value pairs and functions may or may not return a value. Also, objects in javascript are enclosed between curly braces({ and }). When compared to real life, a mobile phone is an object with attributes such as color, size, weight etc. and functions such as switchOn, switchOff, call, touch etc. Objects are assigned to variables just like ordinary variables. An example of an object isRead More →