{
var globalVar = 'Global var'
}
{
let globalLet = 'Global let' // Scope local
}
console.log(globalVar)
console.log(globalLet)
const a = 'b'
a = 'c'
console.log(a)
function newFunction(name = 'Víctor', age = 27, country = 'Chile') {
console.log(name, age, country)
}
newFunction()
newFunction('Vic', 15, 'CL')
const phrase = `${'Hello'} ${'World!'}`
console.log(phrase)
const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud victor exercitation ullamco laboris
ut aliquip ex ea commodo consequat. Duis aute irure dolor`
console.log(lorem)
const person = {
'name': 'Vic',
'age': 27,
'country': 'Chile'
}
console.log(person.name, person.age, person.country)
const {name, age, country} = person
console.log(name, age, country)
const team1 = ['Victor', 'Nacho']
const team2 = ['Maria', 'Pepa', 'Flor']
const team3 = ['Percy', ...team2, ...team1]
console.log(team3)
const name = 'Vic'
const age = 50
//es5
const obj1 = {name: name, age: age}
//es6
const obj2 = {name, age}
console.log(obj2)
const people = [
{name: 'Vic', age: 27},
{name: 'Juan', age: 24}
]
const listOfNames = people.map(function (person) {
return person.name
})
const listOfNames2 = people.map(person => person.name)
console.log(listOfNames)
console.log(listOfNames2)
const square = num => num*num
console.log(square(4))
const helloPromise = (value) => {
return new Promise((resolve,reject) => {
if(value) {
resolve('OK')
} else {
reject('Ups!')
}
})
}
helloPromise(true)
.then(response => console.log(response))
.then(() => console.log('Hey there'))
.catch(error => console.log(error))
helloPromise(false)
.then(response => console.log(response))
.then(() => console.log('Hey there'))
.catch(error => console.log(error))
class calculator {
constructor(valA = 0, valB = 0) {
this.valueA = valA
this.valueB = valB
}
sum() {
return this.valueA + this.valueB
}
}
const calc = new calculator(2,4)
console.log(calc.sum())
import { hello } from './modulo'
hello()
function* helloWorld() {
if(true){
yield 'Hello, '
}
if(true) {
yield 'World'
}
}
const generatorHello = helloWorld()
console.log(generatorHello.next().value)
console.log(generatorHello.next().value)
console.log(generatorHello.next().value)
const numbers = [1, 2, 3, 5, 6, 7, 8, 9]
if(numbers.includes(7)){
console.log('I found it')
}else{
console.log('I did not find it')
}
const base = 4
const exponent = 3
const result = base**exponent
console.log(result)
const data = {
frontend: 'Juan',
backend: 'Victor',
design: 'Johan'
}
const entries = Object.entries(data)
console.log(entries)
console.log(entries.length)
const data = {
frontend: 'Juan',
backend: 'Victor',
design: 'Johan'
}
const values = Object.values(data)
console.log(values)
console.log(values.length)
const string = 'Hello'
console.log(string.padStart(12, '_'))
console.log(string.padEnd(12, '_'))
const comma = {
name: 'Dan',
}
const helloWorldPromise = (value) => {
return new Promise((resolve,reject)=>{
if(value) {
setTimeout(() => resolve('Hello world'), 3000)
} else {
reject(new Error('Test error'))
}
})
}
const helloAsync = async (value) => {
const hello = await helloWorldPromise(value)
console.log('After await')
console.log(hello)
}
helloAsync(true)
const helloWorldPromise = (value) => {
return new Promise((resolve,reject)=>{
if(value) {
setTimeout(() => resolve('Hello world'), 3000)
} else {
reject(new Error('Test error'))
}
})
}
const helloAsync = async (value) => {
try {
const hello = await helloWorldPromise(value)
console.log(hello)
} catch (error) {
console.error(error)
}
}
helloAsync(false)
const obj = {
name: 'Victor',
age: 27,
country: 'Chile'
}
const {country, ...all } = obj
console.log(all)
- Concatenate items in objects
const obj = {
name: 'Victor',
age: 27
}
const obj2 = {
...obj,
country: 'Chile'
}
console.log(obj2)
const obj3 = {
...obj2,
name: 'Juan'
}
console.log(obj3)
const helloWorldPromise = (value) => {
return new Promise((resolve,reject)=>{
if(value) {
setTimeout(() => resolve('Hello world'), 3000)
} else {
reject(new Error('Test error'))
}
})
}
helloWorldPromise(true)
.then(response => console.log(response))
.catch(error => console.log(error))
.finally(() => console.log('ended'))
const regexData = /([0-9]{4})-([0-9]{2})-([0-9]{2})/
const match = regexData.exec('2020-04-20')
const year = match[1]
const month = match[2]
const day = match[3]
console.log(year,month,day)
const array = [1,2,3, [4,5,6, [4,9,0]]]
console.log(array.flat()) // default value is 1
console.log(array.flat(2))
const array2 = [1,2,3,4,5]
console.log(array2.flatMap(value => [value,value*2]))
const hello = ' hello world '
console.log(hello.trimStart())
console.log(hello.trimEnd())
console.log(hello.trim())
try{
} catch(error) {
console.log(error)
}
// is same
try{
} catch {
console.log(error)
}
const entries = [
['name', 'Victor'], ['age', 27]
]
console.log(Object.fromEntries(entries))
- Description in symbol objects
const mySymbol = `My symbol`
const symbol = Symbol(mySymbol)
console.log(symbol.description)