> For the complete documentation index, see [llms.txt](https://www.learnrxjs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.learnrxjs.io/learn-rxjs/operators/filtering/takewhile.md).

# takeWhile

#### signature: `takeWhile(predicate: function(value, index): boolean, inclusive?: boolean): Observable`

## Emit values until provided expression is false.

***

💡 When the optional `inclusive` parameter is set to `true` it will also emit the first item that didn't pass the predicate.

***

### Examples

**Example 1: Take values under limit**

( [StackBlitz](https://stackblitz.com/edit/typescript-af3hdf?file=index.ts\&devtoolsheight=100) | [jsBin](http://jsbin.com/zanefaqexu/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/yakd4jgc/) )

```js
// RxJS v6+
import { of } from 'rxjs';
import { takeWhile } from 'rxjs/operators';

//emit 1,2,3,4,5
const source$ = of(1, 2, 3, 4, 5);

//allow values until value from source is greater than 4, then complete
source$
  .pipe(takeWhile(val => val <= 4))
  // log: 1,2,3,4
  .subscribe(val => console.log(val));
```

**Example 2: (v6.4+) takeWhile with inclusive flag**

( [StackBlitz](https://stackblitz.com/edit/typescript-3bwfup?file=index.ts\&devtoolsheight=100) )

```js
// RxJS v6.4+
import { of } from 'rxjs';
import { takeWhile, filter } from 'rxjs/operators';

const source$ = of(1, 2, 3, 9);

source$
  // with inclusive flag, the value causing the predicate to return false will also be emitted
  .pipe(takeWhile(val => val <= 3, true))
  // log: 1, 2, 3, 9
  .subscribe(console.log);
```

**Example 3: Difference between `takeWhile` and** [**`filter`**](/learn-rxjs/operators/filtering/filter.md)

( [StackBlitz](https://stackblitz.com/edit/typescript-roozza?file=index.ts\&devtoolsheight=100) | [jsBin](http://jsbin.com/yatoqurewi/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/r497jgw3/4/) )

```js
// RxJS v6+
import { of } from 'rxjs';
import { takeWhile, filter } from 'rxjs/operators';

// emit 3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3
const source$ = of(3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3);

// allow values until value from source equals 3, then complete
source$
  .pipe(takeWhile(it => it === 3))
  // log: 3, 3, 3
  .subscribe(val => console.log('takeWhile', val));

source$
  .pipe(filter(it => it === 3))
  // log: 3, 3, 3, 3, 3, 3, 3
  .subscribe(val => console.log('filter', val));
```

### Related Recipes

* [Alphabet Invasion Game](/learn-rxjs/recipes/alphabet-invasion-game.md)
* [Battleship Game](/learn-rxjs/recipes/battleship-game.md)
* [Breakout Game](/learn-rxjs/recipes/breakout-game.md)
* [Car Racing Game](/learn-rxjs/recipes/car-racing-game.md)
* [Catch The Dot Game](/learn-rxjs/recipes/catch-the-dot-game.md)
* [Click Ninja Game](/learn-rxjs/recipes/click-ninja-game.md)
* [Flappy Bird Game](/learn-rxjs/recipes/flappy-bird-game.md)
* [Mine Sweeper Game](/learn-rxjs/recipes/mine-sweeper-game.md)
* [Platform Jumper Game](/learn-rxjs/recipes/platform-jumper-game.md)
* [Smart Counter](/learn-rxjs/recipes/smartcounter.md)
* [Swipe To Refresh](/learn-rxjs/recipes/swipe-to-refresh.md)
* [Tetris Game](/learn-rxjs/recipes/tetris-game.md)
* [Uncover Image Game](/learn-rxjs/recipes/uncover-image-game.md)

### Additional Resources

* [takeWhile](https://rxjs-dev.firebaseapp.com/api/operators/takeWhile) 📰 - Official docs
* [Completing a stream with takeWhile](https://egghead.io/lessons/rxjs-completing-a-stream-with-takewhile?course=step-by-step-async-javascript-with-rxjs) 🎥 💵 - John Linquist

***

> 📁 Source Code: <https://github.com/ReactiveX/rxjs/blob/master/packages/rxjs/src/internal/operators/takeWhile.ts>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.learnrxjs.io/learn-rxjs/operators/filtering/takewhile.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
