Ecmascript 2015 (ES6) - Part 1

Block scope
Using let or const keyword to declare scope variables, these variables have scope in {} block.
Value of a const must be an object and it's not able to change.

if (true) {
  const info = { lang: "ruby", domain : "rubysnippets.net" };
  let temp = 'Ecmascript'
}
// raise an error: temp is not defined
console.log(temp)    

Function syntax

var functionName = (var1, var2) => {
  // Magic code
};

Default variable

var $domain = $domain || 'http://rubysnippets.net'
console.log($domain);     
//=> http://rubysnippets.net

Rest parameter

let car = (color, speed, ...other) => {
  console.log("Color: " + color);
  console.log("Speed: " + speed);
  console.log("Other:");
  console.log(other);
}

car('red', '100km/h', 'new', '$15000', '800kg');

/*
Color: red
Speed: 100km/h
Other:
["new", "$15000", "800kg"]
*/

Set
The Set object lets you store unique values of any type

// Init (param must be an array)
var numbers = new Set([1, 2, 3, 4]);

// Add element
numbers.add(5);      // Set {1, 2, 3, 4 ,5}

// Remove element
numbers.delete(1);   // Set {2, 3, 4 ,5}

// Check is Set has an element
numbers.has(2);      // True

// Count
numbers.size;        // 4

// Remove all elements
numbers.clear();

// Loop
for (let number of numbers){
  console.log(number);
}

// Parse Set to Array
var arr = [...numbers];

Collection Maps
The Map object is similar to Set but it use key/value

// Init
let map = new Map([ ["Name", "admin"], ["Email", "admin@rubysnippets.net"] ]);

// Add element
map.set('Age', '25');     // Map { "Name" => "admin", "Email", "admin@rubysnippets.net", "Age", "25" }

// Get list of Keys or Values
map.keys()
map.values()

// Loop
map.forEach((value, key) => {
  console.log(key + ' - ' + value);
});

// Filter
let new_map = new Map( [...map].filter( ([key, value]) => 
  condition
));

// It's can be use methods: delete, has, size, clear as well