Block-scoped. Cannot be re-assigned. Primitive values are immutable.
const test = 'test';
const test = { message: 'Hello World' };
test.message = 'Goodbye World';
let i = 0;
// Creating the promise
const p = new Promise(function(resolve, reject) {
if (/* condition */) {
resolve(/* value */);
} else {
reject(/* reason */)
}
});
// Consuming the promise
p
.then((data) => console.log(data))
.catch((err) => console.log(err));
const arrow = (dart, bow) => console.log('Implicit return');
const arrow = dart => console.log('One parameter');
const arrow = () => console.log('No parameters');
const arrow = () => ({ foo: 'bar' }); // Returns an object literal
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5));
// 5
console.log(multiply(5, 3));
// 15
const name = 'Melanie';
console.log(`Hello, ${name}!`);
// Hello, Melanie!
const name = 'Melanie';
const messages = 5;
function hello(strings, ...values) {
console.log('Strings:', strings);
console.log('Values:' values);
}
const welcome = hello`Hello, ${name}! You have ${messages} new messages.`
// Strings: ['Hello, ', '! You have ', ' new messages.']
// Values: ['Melanie', '5']
// Array.from
const navItems = document.querySelectorAll('.nav li');
const navItemsArray = Array.from(navItems);
// Array.of
const ages = Array.of(5, 15, 20, 3);
const ids = [5, 45, 30];
// Array.find
const find = ids.find(x => x <= 10);
console.log(find);
// 5
// Array.findIndex
const find = ids.findIndex(x => x >= 40);
console.log(find);
// 1
const str = 'Coding is awesome';
console.log(str.startsWith('C'));
// Returns true
console.log(str.endsWith('a', 6));
// Returns false
console.log(str.includes('awe'));
// Returns true
const rock = '🚀';
console.log(rock.repeat(10));
// 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
const obj = { title: 'Dr', name: 'McDreamy'};
function giveLove({ title, name }) {
console.log(`${title} ${name} is sexy`);
}
console.log(giveLove(obj));
// Dr McDreamy is sexy
// Renaming props
const user = { theName: 'Melanie' };
const { theName: name } = user;
console.log(name);
// Melanie
let user = {
education: {
degree: {
name: 'Bachelor',
field: 'Multimedia'
},
country: 'Australia'
}
};
let {
education: {
degree: {
name,
field
},
country
}
} = user;
console.log(`Studied ${name} of ${field} in ${country}`);
// Studied Bachelor of Multimedia in Australia
const arr = ['Melanie', 1337];
const [name, coolness] = arr;
console.log(`${name} is ${coolness} 😎`);
// Melanie is 1337 😎
// Ignore values
const [, coolness] = arr;
console.log(coolness);
// 1337
console.log(player, benched); // Steve, John
[player, benched] = [benched, player];
console.log(player, benched); // John, Steve
for (const item of items) {
console.log(item);
}
const people = ['Sara', 'John'];
const cats = ['Callie'];
const myFamily = [...people, ...cats];
console.log(myFamily);
// myFamily is ['Sara', 'John', 'Callie']
function multiply(rate, ...numbers) {
return numbers.map(number => number * rate);
}
console.log(multiply(2, 10, 20, 30));
// 20, 40, 60
const myMap = new Map(); // creates a new instance of a Map object
const keyObj = {};
console.log(myMap); // Map(0) {}
// unlike object-literals, a Map's keys/values can be anything
myMap.set('string', 'they can be strings')
myMap.set(1, 'or integers')
myMap.set(true, '...booleans')
myMap.set(keyObj, 'even objects?')
console.log(myMap.size) // 4
myMap.get(keyObj) // "even objects?"