A Guide for Picking Up Node.js Development for PHP developers

ES6 (ES2015) and node version 8+

1. let and const instead of var

// Old JS (still works, but almost no one uses this anymore)
var foo_1 = 'bar';
// The ES6 / node 8+ way
let foo_2 = 'bar';
foo_2 = 'bar2'; // re-assign its value
// Consts values can't change
const foo_3 = 'bar';
foo_3 = 'bar2'; // this gives an error
const foo_1 = getFoo(1); // This is fine
const foo_2 = { hello: 'world' }; // This is fine
foo_2.hello = 'earth'; // This is fine too!

2. Template literals / template strings

const str = 'world';
console.log(`Hello ${str}!`); // prints Hello world!
// The old way would have been...
console.log('Hello ' + str + '!');
// You can put entire expressions within them like a function call
// just like you would anywhere else:
console.log(`Hello ${getStr()}!`);

3. require and module.exports

// File one: getHello.js
module.exports = function(param) {
return `Hello ${param}!`;
}
// File two: index.js
const getHello = require('./getHello');
console.log(getHello('world')); // prints Hello world!

4. Arrow functions (fat-arrow functions)

// Defining a function and giving it a name
const myFunc = (param1, param2) => {
return 'Hello!';
};
// If there is only one param, even the parenthesis is optional!
// This can look confusing at first. It works the same way.
const myFunc2 = param1 => {
return 'Hello!';
};
// Passing in a function as a parameter, such as a callback.
// Note the lack of parameters here, which is valid.
setTimeout(() => { console.log('Hi'); }, 1000);
const getHello = param => `Hello ${param}!`; // Valid function!console.log(getHello('world')); // prints Hello world!

5. Object and array destructuring

// Example of an object. This can be returned from a function, etc.
const obj = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5
};
// You just need one and four? Sure!
const { one, four } = obj;
// Now the consts "one" and "four" are ready to be used.// Alternatively, name them something else as you declare them:
const { one: foo, four: bar } = obj;
// Now foo = 1, and bar = 4!// Similar things can be done with arrays
const arr = [1, 2, 3, 4, 5];
const [one, two] = arr; // one = 1, two = 2
function doStuff({one, two, three}) {
console.log(`Hey ${one} ho ${two} and ${three}!`);
}
// This allows you to pass in arguments in a more flexible manner,
// and also be more clear what you're passing in:
doStuff({three: 'foo', two: 'bar'});

6. Enhanced Object Literals

const one = 1;
const two = 2;
const three = 3;
const myObj = { one, two, three };
// This is equivalent to:
// { one: one, two: two, three: three }
const f = 'foo';
const myObj = {
[f + 'bar']: true
};
// myObj is now { foobar: true }

7. Promises

// consider doing two http requests sequentially in callbacks...
const request = require('request');
request('http://www.google.com', function (err, resp, body) {
request('http://www.yahoo.com', function (err, resp, body) {
// do stuff with the result
});
});
// similar code but in promise...
const requestpromise = require('request-promise'); // different lib
requestpromise('http://www.google.com')
.then(function(body) {
return requestpromise('http://www.yahoo.com');
})
.then(function(body) {
// do stuff with the result
})
.catch(function(err) {
// error handling
});
const rp = require('request-promise');
Promise.all([
rp('http://www.google.com'),
rp('http://www.yahoo.com')
])
.then(function([body1, body2]) {
// do stuff with both response bodies
})
.catch(function(err) {
// this catches any error with either of the calls
});

8. Async/await

const rp = require('request-promise');// Anything that returns a Promise can be await'd.
const body1 = await rp('http://www.google.com');
const body2 = await rp('http://www.yahoo.com');
// do stuff with body1 and body2
// You can still use Promise.all() to run them in parallel:
const [body1, body2] = await Promise.all([
rp('http://www.google.com'),
rp('http://www.yahoo.com')
]);
// Functions that contain "await" in its body need to be declared
// as "async" explicitly.
async function doStuff() {
const body = await rp('http://www.google.com');
// do stuff
}
// async functions are automatically implied to return a promise,
// so calling async functions will need an await:
await doStuff();
// Since that's just a regular promise, you can run in parallel too:
await Promise.all([
doStuff(),
rp('http://www.yahoo.com')
]);
// Error handling looks like this:
try {
await doStuff();
} catch (err) {
// error handling here
}

--

--

--

Lead Engineer @knockdotcom. Previously, Principal @drop, CTO @CoinTent, Architect @Zynga.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JavaScript’s Filter Method

React Devstack

Appsync + Angular 7 + Amplify + Cognito- Part 3

How to add Sitemaps to a React Gatsby Static Site and Google Search Console

My Node.js Interview was Disastrous Because I Lacked this Basic Knowledge

Connecting Algolia to Cosmic JS for awesome search functionality!

React: What Are Hooks and Why Should You Use Them?

Mutable & Immutable Array Methods in JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bigi Lui

Bigi Lui

Lead Engineer @knockdotcom. Previously, Principal @drop, CTO @CoinTent, Architect @Zynga.

More from Medium

5 NPM Packages for Node.js Developers in 2022

How to Use a JavaScript Reporting Tool in a GoLang Web App

What is Node.js and Why You Should Use Node.js as Your Backend 2022

Using Nginx as Load Balancer with Node.js (Express)