Found 6710 Articles for Javascript

What is the difference between for...in and for...of loops in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Difference between for...in and for...of loopsBoth the loops iterate over something. The main difference between them is in what they iterate over.1) for...in loopThis loop iterates over enumerable properties of an object in an arbitrary order. It cares only about properties but not values.In the following example by using for...in loop the properties of the array are iterated. Since it is an array, Index is an important property so index of each and every element will be iterated and displayed in the output. In addition to indexes some inherited properties such as "inherProp2", "inherProp1" are also displayed.Example-1Live Demo   ... Read More

How to remove html tags from a string in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

7K+ Views

Removing HTML tags from a stringWe can remove HTML/XML tags in a string using regular expressions in javascript. HTML elements such as span, div etc. are present between left and right arrows for instance , etc. So replacing the content within the arrows, along with the arrows, with nothing('') can make our task easy.Syntaxstr.replace( /(]+)>)/ig, '');Example-1Live Demo function removeTags(str) { if ((str===null) || (str==='')) return false; else str = str.toString(); ... Read More

What is global namespace pollution in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 08:55:29

1K+ Views

Global namespace pollutionPolluting Global namespace causes name collision. This name collision is very common in large projects where we may be using several javascript libraries. Let's discuss in detail what a name collision is.let's take a scenario in which 2 teams named A1 and A2 are working on a project. They both prepared their own javascript files that is TeamA1.js and TeamA2.js as shown below.TeamA1.jsTeamA1 has created a student function and has 2 parameters fname and lname(firstname & lastname).function student(fname, lname){    this.fname = fname;    this.lname = lname;    this.getFullName = function (){       return this.fname + " " ... Read More

What is the main difference between objects created using object literal and constructor function?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script. Let's discuss them individually.1) Objects created using object literalSince these are singletons, a change to an object persists throughout the script. A change in one instance will affect all the instances of the object. In the following example if we observe, both the objects "student" and "newStudent" display the same name(Ravi) initially. But ... Read More

How to get a part of string after a specified character in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

15K+ Views

To get a part of a string, string.substring() method is used in javascript. Using this method we can get any part of a string that is before or after a particular character.str.substring() This method slices a string from a given start index(including) to end index(excluding). If only one index is provided then the method slice the entire string from the start of the index. syntax-1Using this line of code we can get a part of a string after a particular character.string.substring(string.indexOf(character) + 1);syntax-2Using this line of code we can get a part of a string before a particular character.string.substring(0, string.indexOf(character));ExampleLive Demo ... Read More

What is function chaining in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 08:40:24

4K+ Views

Function chainingFunction chaining is nothing but grouping functions in one single line using dot notation. This type of chaining makes the code very concise and also improves the performance.Here we are going to learn function chaining using regular objects.a) Without function chaining In the following example an object 'obj' is created and in that object a public property called 'i' is created using keyword 'this' and initially assigned a value 0. later on user defined functions called add(), subtract() and print() are created inside the same object 'obj'. Now, the object "obj" will acts like a class(it's properties can be shared by ... Read More

How to get a number of vowels in a string in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Calculating number of vowels in a stringVowels in English language are a, e, i, o and u. Make sure that, in any string these vowels can be both cases ( either small or capital). DebriefThe following example, using a user defined function called 'noOfVowels()', reads an input string and compares that string with another string which contains only vowels( 'aAeEiIoOuU'). It takes the help of indexOf() method to proceed the task. The indexOf() method displays index of a character whenever the character is common to both the strings, in unmatched case it displays '-1' as the output. Here it compares each and ... Read More

How to Check if a Variable is an Array in JavaScript?

vineeth.mariserla
Updated on 20-Nov-2024 17:11:18

4K+ Views

To check if a variable is an array in Javascript is essential to handle data appropriately. We will discuss three different approaches to check if a variable is an array or not. We are having an array and a string, and our task is to check if a variable is an array in JavaScript. Approaches to Check if a Variable is an Array Here is a list of approaches to check if a variable is an array in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes. Using ... Read More

How to check whether a key exist in JavaScript object or not?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

330 Views

There are a couple of ways to find whether a key exist in javascript object or not.Let's say we have an 'employee' object as shown below.   var employee = {       name: "Ranjan",       age: 25    }Now we need to check whether 'name' property exist in employee object or not.1) 'In' operatorWe can use 'in' operator on object to check its properties. The 'in' operator also looks about inherited property if it does not find any actual properties of the object.In the following example when 'toString' is checked whether present or not, the 'in' operator ... Read More

How to empty an array in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

849 Views

There are a couple of ways to empty an array in javascript.Let's suppose take an arrayvar array1 = [1, 2, 3, 4, 5, 6, 7];Method 1var array1 = [];The code above will set the number array to a new empty array. This is recommended when you don't have any references to the original array 'array1'. You should be careful with this way of empty the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.Example var array1 = [1, 2, 3, 4, 5, 6, 7];  // ... Read More

Advertisements