---
title: "ES6 on Node.js"
description: "Using ES6 harmony features in Node.js with --harmony flags. Practical notes and code examples on ES6 on Node.js by Hemanth."
url: "https://h3manth.com/scribe/es6-on-nodejs/"
canonical_url: "https://h3manth.com/scribe/es6-on-nodejs/"
markdown_url: "https://h3manth.com/scribe/blog/es6-on-nodejs.md"
publishDate: "2013-09-01T00:00:00.000Z"
author: "Hemanth HM"
category: "node"
tags: ["node","javascript","es6"]
word_count: 166
reading_time_minutes: 1
x_markdown_tokens: 387
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# ES6 on Node.js

> Using ES6 harmony features in Node.js with --harmony flags. Practical notes and code examples on ES6 on Node.js by Hemanth.

Here I demonstrate ES6 (harmony) features using raw Node.js (v0.11.6).

## Enabling Harmony Features

```bash
$ node --v8-options | grep harm
--harmony_typeof
--harmony_scoping
--harmony_modules
--harmony_symbols
--harmony_proxies
--harmony_collections
--harmony_generators
--harmony_iteration
--harmony_numeric_literals
--harmony_strings
--harmony_arrays
--harmony (enable all harmony features)

Enable all features with strict mode:
bash
$ node --use-strict $(node --v8-options | grep harm | awk '{print $1}' | xargs)
```

## Block Scoping with `let`

```javascript
function aboutme() {
  {
    let gfs = 10;
    var wife = 1;
  }
  console.log(wife); // 1
  console.log(gfs);  // ReferenceError: gfs is not defined
}
```

`let` defines a variable in the current block, unlike `var` which is function-scoped.

## Generators

Generators help build iterators. `yield` suspends execution:

```javascript
function* Counter() {
  var n = 0;
  while (1 < 2) {
    yield n;
    ++n;
  }
}

var CountIter = new Counter();
CountIter.next() // { value: 0, done: false }
CountIter.next() // { value: 1, done: false }
```

The `done` attribute will be `true` once the generator has nothing more to yield.
