Listado de la etiqueta: Methods


JavaScript has many useful methods that can work easily with the arrays. Some of these are map(), pop(), filter() and push(). JavaScript also has some() and every() methods. The main difference between the mentioned methods is that the some() method is used for finding at least one or more than one value in the array according to the passed condition, whereas the every() method checks whether all elements of an array are satisfying the given condition or not.

This post will practically demonstrate the difference between every() and some() method using appropriate examples. So, let’s start!

every() Method in JavaScript

every() method in JavaScript is used to check whether all elements of an array are satisfying the given condition or not. If even a single value does not satisfy the element the output will be false otherwise it will return true. It is opposed to some() method.

Syntax

The universal syntax of every() method is:

array.every(callback(currentvalue, index, arr), thisArg)

In JavaScript, every() method returns a Boolean value (true/false) as output.

Parameters

  • callback” is a function that will test the condition.
  • currentvalue” shows the current element of the array and it is required.
  • index” represents the index of the current element of the array and it is optional.
  • arr” is an optional parameter and demonstrates the array where the current element belongs.
  • thisArg” is an optional parameter and its value is used while executing the callback function.

Now, let’s check out an example for understanding the usage of every() method in JavaScript.

How to use every() method in JavaScript

In this section, we will demonstrate the usage of every() method in JavaScript. For this purpose, consider the following array of integer values:

let arr = [1, 2, 3, 4, 5, 6, 7, 8 ];

We will now use every() method to check whether the given array has a positive value or not:

arr.every((value)=> {

return (value > 0);

});

The given array that we passed to the every() method has positive values so the condition is satisfied and the output will be true otherwise it will return false as an output if the given condition is not satisfied:

some() Method in JavaScript

The some() method is used with arrays in JavaScript. It accepts the Boolean expression (true/false) and is used to check if at least one or more than one element in the array satisfies the passed condition or not.

Syntax

The universal syntax of some() method is:

array.some(function(value, index, arr), this)

In JavaScript, some() method also returns a Boolean value (true/false) as output.

Parameters

  • function” executes for every element of the array.
  • value” shows the current element of the array and it is required.
  • index” refers to the index of the current array element and is an optional parameter.
  • arr” refers to the array where the current element belongs and it is also an optional parameter.

These parameters are optional and the boolean expression that it accepts is as follows:

The “element” denotes the current element in the array that is being checked. The “boolean” returns the Boolean value either true or false.

How to use some() method in JavaScript

Now, consider the following array of integer values:

let arr =[ 2, 3, 4, 5, 6, 7, 8];

Next, we will check if there is at least or more than one even element is in the array by using the some() method:

arr.some((value) => { return (value%2 == 0); });

The some() method will find at least or more than one even element from a given array and the output will be true because the given list has four even elements that are divisible by 2:

We have disscussed the difference between some() and every() method, their syntax and example.

Conclusion

In JavaScript, main difference between the every() and some() methods is that the some() method is used for finding at least one or more than one value in the array according to the passed condition, whereas, the every() method check whether all elements of an array are satisfying the given condition or not. This post illustrates the difference between every() and some() methods in JavaScript, its syntax with examples.



Source link


Set Objects in JavaScript are very much similar to the arrays the major difference between these two is that the set objects can’t hold the duplicate values however arrays can. In JavaScript, the set objects allow us to store only unique values in them. These unique values can be of any type like primitive data types(int, strings, etc), object references, complex object/data types such as object, arrays, literals, etc. In JavaScript several set object methods can be used to achieve different functionalities, for example, add(), delete(), forEach(), etc.

In this article, we will cover the below listed aspects of set object methods in JavaScript:

So, let’s begin!

How to use new Set() to create a set in JavaScript

To work with any of the Set object methods, firstly, we have to create a set. To do so, we can use the “new Set()” constructor.

Example

The below given piece of code will explain how to create a Set in JavaScript using the “new Set()” constructor:

<script>
let employeeNames = new Set();

console.log(employeeNames);
</script>

The above code will create an empty set as shown in the following output:

The output shows that an empty Set is created, now we can perform any functionality on that set using different set object methods such as append elements, remove elements, etc.

How to use add() method to add elements in a set

JavaScript provides a method named add() that is used to append the elements in a set.

Example

Now, we will extend the above example a little bit more to add/append the elements to the Set:

employeeNames.add(«Steve»);
employeeNames.add(«Michael»);
employeeNames.add(«Smith»);
employeeNames.add(«Paul»);
employeeNames.add(«Ambrose»);
console.log(employeeNames);

In this example, we added five elements in the set named “employeeNames” using the add() method. Afterward, we utilized the console.log() method to print all the elements stored in the “employeeNames” set on the browser’s console:

The output verifies the working of the add() method.

How to use delete() method to remove elements from a set

In JavaScript, the delete() method can be used to remove some specific elements from the set object.

Example

Let’s suppose we want to remove “Smith”, and “Paul” from the set “employeeNames”. To do so, we can utilize the delete() method:

employeeNames.delete(«Smith»);
employeeNames.delete(«Paul»);
console.log(employeeNames);

The above code block will generate the following output:

The above snippet shows that the delete() method has removed “Smith” and “Paul” from the Set successfully.

How to use clear() method to delete all elements from a set

In JavaScript, the clear() method can be used to remove all the elements of a set object.

Example

In this example, we will utilize the clear() method to delete all the items from the set “employeeNames”:

employeeNames.clear();
console.log(employeeNames);

The above code snippet will produce the following results:

The output shows the Set’s size equal to zero; it authenticates the working of the clear() method.

How to check the existence of some specific value in a set using has() method

In JavaScript, the has() method can be used to check whether a specific element exists in the set or not.

Example

In this example, we will check the existence of two elements i.e. “Smith”, and “Bryn” using the has() method:

console.log(employeeNames.has(«Smith»));
console.log(employeeNames.has(«bryn»));

The piece of code will return true if the specified value exists in the set and it will return false if the specified value doesn’t exist in the targeted set:

The output shows that the has() method returns true for the element “smith” as it exists in the set while “Bryn” doesn’t exist in the targeted set therefore the has() method returns false for it.

How to find the size of a set

In JavaScript, the size property can be used to check the size/length of some specific set.

Example

In this example we will utilize the size property to check the size of the set “employeeNames”:

Following will be the corresponding output for the above-given code:

The output shows the appropriate size of the specified set i.e “employeeNames” set.

Conclusion

In JavaScript, the set objects allow us to store only unique values in them. These unique values can be of any type like primitive data types(int, strings, etc), object references, complex object/data types such as objects, arrays, literals, etc. In JavaScript, there is a wide range of set object methods that can be used to perform different functionalities. For example, Add(), delete(), and clear(), methods are used to append and remove elements, the has() method is used to check the existence of an element in a specific set, etc. This write-up explained the working of various set object methods with the help of suitable examples.



Source link