Setup Project

  • mkdir /Fundamentals
  • cd Fundamentals
  • npm init
    • defaults
  • npm install –save-dev babel-cli
  • npm install –save-dev babel-preset-es2015 babel-preset-stage-2
  • touch variables.js
  • atom .

package.json

{
“name”: “Fundamentals”,
“version”: “1.0.0”,
“description”: “”,
“main”: “variables.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1″,
“start”: “nodemon variables.js –exec babel-node –presets es2015,stage-2”
},
“author”: “”,
“license”: “ISC”,
“devDependencies”: {

}
}
[/code]

variables.js

"use strict";

var firstname = "Jon";
var lastname = "Doe";
var age = 21;
var married = false;
var sex = 'M'

console.log(firstname);
console.log(age);
console.log(married);
console.log(sex);

console.log(firstname+" "+lastname); // ES5
console.log(firstname,lastname); // ES5

console.log(`${firstname} ${lastname}`); // ES6
console.log(`Fullname: ${firstname} ${lastname} - (${sex}) ${age} - Married: ${married}`); // ES6

var fullname = `${firstname} ${lastname}` // ES6
console.log(fullname);

// let
var people = ['Jon Doe', 'Jane Doe', 'Mick Doe'];

// Pollutes global namespace.
for (var i = 1; i <= people.length; i += 1) {
    console.log(`person ${i} is ${people[i-1]}.`);
}

console.log(i); // 4

// Only in scope in the for
for (let j = 1; j <= people.length; j += 1) {
  console.log(`person ${j} is ${people[j-1]}.`);
}

try {
    console.log(j); // Reference error
} catch (err) {
    console.log(`Error: ${err}.`);
}
// const

const PI = 3.142

console.log(PI);

// PI = 6; // Syntax error - PI is readonly

Better to use let and const instead of var

Start Node and run the code

  • npm start
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s