Syntax

JavaScript Filter an Array Syntax

In JavaScript, filter an array: keeps only items that pass a test — nums.filter((n) => n > 0)

Quick Answer

Syntax
nums.filter((n) => n > 0)
JavaScriptSyntax

Filter an Array

Code example
nums.filter((n) => n > 0)

Keeps only items that pass a test.

Learn More

How to filter an array in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Filter an Array Syntax

Use nums.filter((n) => n > 0).

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for filter an array?

Use this pattern: nums.filter((n) => n > 0)

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify