Javascript array includes

The Array.prototype.includes() method is used in JavaScript to check whether an array contains a specific element. It returns true if the element is found in the array, and false otherwise. The method performs a strict equality comparison (===) to determine the presence of the element.

Here's the basic syntax of includes():

array.includes(element)

Where:

  • array is the array on which you want to perform the search.
  • element is the value you want to check for presence in the array.

Here's an example usage:

const numbers = [1, 2, 3, 4, 5];

console.log(numbers.includes(3));  // true
console.log(numbers.includes(6));  // false

In this example, includes(3) returns true because the number 3 is present in the numbers array. On the other hand, includes(6) returns false since 6 is not present in the array.

You can also specify an optional second parameter, fromIndex, which indicates the index at which the search should start. If provided, the search will begin at that index and iterate towards the end of the array. Here's an example:

const numbers = [1, 2, 3, 4, 5];

console.log(numbers.includes(2, 2));  // false
console.log(numbers.includes(2, 1));  // true

In this case, includes(2, 2) returns false because it starts the search at index 2 and doesn't find the element 2. However, includes(2, 1) returns true because it starts the search at index 1 and finds the element 2.

Note that the includes() method was introduced in ECMAScript 2016 (ES7), so make sure you're using a compatible version of JavaScript.

댓글

이 블로그의 인기 게시물

PYTHONPATH, Python 모듈 환경설정

You can use Sublime Text from the command line by utilizing the subl command

git 명령어

[gRPC] server of Java and client of Typescript

[Ubuntu] Apache2.4.x 설치

Create topic on Kafka with partition count, 카프카 토픽 생성하기

리눅스의 부팅과정 (프로세스, 서비스 관리)

Auto-populate a calendar in an MUI (Material-UI) TextField component

The pierce selector in Puppeteer