---
title: Flux syntax basics
description: An introduction to the basic elements of the Flux syntax with real-world application examples.
v2.0/tags: [flux, syntax]
menu:
  v2_0:
    name: Syntax basics
    parent: Get started with Flux
weight: 203
---


Flux, at its core, is a scripting language designed specifically for working with data.
This guide walks through a handful of simple expressions and how they are handled in Flux.

## Use the influx CLI's REPL
Use the `influx repl` command to open the interactive read-eval-print-loop (REPL).
Run the commands provided in this guide in the REPL.

##### Start in the influx CLI in Flux mode
```bash
influx repl --org org-name
```

## Basic Flux syntax
The code blocks below provide commands that illustrate the basic syntax of Flux.
Run these commands in the REPL.

### Simple expressions
Flux is a scripting language that supports basic expressions.
For example, simple addition:

```js
> 1 + 1
2
```

### Variables
Assign an expression to a variable using the assignment operator, `=`.

```js
> s = "this is a string"
> i = 1 // an integer
> f = 2.0 // a floating point number
```

Type the name of a variable to print its value:

```js
> s
this is a string
> i
1
> f
2
```

### Objects
Flux also supports objects. Each value in an object can be a different data type.

```js
> o = {name:"Jim", age: 42}
```

Use dot notation to access a properties of an object:

```js
> o.name
Jim
> o.age
42
```

### Lists
Flux supports lists. List values must be the same type.

```js
> n = 4
> l = [1,2,3,n]
> l
[1, 2, 3, 4]
```

### Functions
Flux uses functions for most of its heavy lifting.
Below is a simple function that squares a number, `n`.

```js
> square = (n) => n * n
> square(n:3)
9
```

{{% note %}}
Flux does not support positional arguments or parameters.
Parameters must always be named when calling a function.
{{% /note %}}

### Pipe-forward operator
Flux uses the pipe-forward operator (`|>`) extensively to chain operations together.
After each function or operation, Flux returns a table or collection of tables containing data.
The pipe-forward operator pipes those tables into the next function where they are further processed or manipulated.

```js
data |> someFunction() |> anotherFunction()
```

## Real-world application of basic syntax
This likely seems familiar if you've already been through through the other
[getting started guides](/v2.0/query-data/get-started).
Flux's syntax is inspired by Javascript and other functional scripting languages.
As you begin to apply these basic principles in real-world use cases such as creating data stream variables,
custom functions, etc., the power of Flux and its ability to query and process data will become apparent.

The examples below provide both multi-line and single-line versions of each input command.
Carriage returns in Flux aren't necessary, but do help with readability.
Both single- and multi-line commands can be copied and pasted into the `influx` CLI running in Flux mode.

### Define data stream variables
A common use case for variable assignments in Flux is creating variables for one
or more input data streams.

{{< code-tabs-wrapper >}}
  {{% code-tabs %}}
  [Multi-line](#)
  [Single-line](#)
  {{% /code-tabs %}}
{{% code-tab-content %}}
```js
timeRange = -1h

cpuUsageUser =
  from(bucket:"example-bucket")
    |> range(start: timeRange)
    |> filter(fn: (r) =>
      r._measurement == "cpu" and
      r._field == "usage_user" and
      r.cpu == "cpu-total"
    )

memUsagePercent =
  from(bucket:"example-bucket")
    |> range(start: timeRange)
    |> filter(fn: (r) =>
      r._measurement == "mem" and
      r._field == "used_percent"
    )
```
{{% /code-tab-content %}}

{{% code-tab-content %}}
```js
timeRange = -1h
cpuUsageUser = from(bucket:"example-bucket") |> range(start: timeRange) |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_user" and r.cpu == "cpu-total")
memUsagePercent = from(bucket:"example-bucket") |> range(start: timeRange) |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper>}}

These variables can be used in other functions, such as  `join()`, while keeping the syntax minimal and flexible.

### Define custom functions
Create a function that returns the `N` number rows in the input stream with the highest `_value`s.
To do this, pass the input stream (`tables`) and the number of results to return (`n`) into a custom function.
Then using Flux's `sort()` and `limit()` functions to find the top `n` results in the data set.

{{< code-tabs-wrapper >}}
  {{% code-tabs %}}
  [Multi-line](#)
  [Single-line](#)
  {{% /code-tabs %}}
{{% code-tab-content %}}
```js
topN = (tables=<-, n) =>
  tables
    |> sort(desc: true)
    |> limit(n: n)
```
{{% /code-tab-content %}}
{{% code-tab-content %}}
```js
topN = (tables=<-, n) => tables |> sort(desc: true) |> limit(n: n)
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}

_More information about creating custom functions is available in the [Custom functions](/v2.0/query-data/guides/custom-functions) documentation._

Using the `cpuUsageUser` data stream variable defined above, find the top five data
points with the custom `topN` function and yield the results.

{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[Multi-line](#)
[Single-line](#)
{{% /code-tabs %}}

{{% code-tab-content %}}
```js
cpuUsageUser
  |> topN(n:5)
  |> yield()
```
{{% /code-tab-content %}}

{{% code-tab-content %}}
```js
cpuUsageUser |> topN(n:5) |> yield()
```
{{% /code-tab-content %}}

{{< /code-tabs-wrapper>}}

This query will return the five data points with the highest user CPU usage over the last hour.

<div class="page-nav-btns">
  <a class="btn prev" href="/v2.0/query-data/get-started/transform-data/">Transform your data</a>
</div>
