Yes! This does not throw an error:
var camper = 'James';
var camper = 'David';
- global
- local in function
- global
- function
- block
- statement
- expression
Global
Three different i variables with unique values (0, 1, and 2).
Object.freeze(obj)
const myFunction = () => returnValue;
const doubler = item => item * 2;
const greeting = (name = "Anonymous") => "Hello " + name;
function howMany(...args) {
return "You have passed " + args.length + " arguments.";
}
How can I expand arrays and other expressions in places where multiple parameters or elements are expected?
// Math.max expects comma-separated arguments
Math.max(...arr)
No, ...
works only in place. The assignment above will throw an error.
[...arr]
const user = { name: 'John Doe', age: 34 };
const { name, age } = user;
How do I use new variable names when extracting values from an object using the destructuring assignment?
const { name: userName, age: userAge } = user;
const user = {
johnDoe: {
age: 34,
email: 'johnDoe@freeCodeCamp.com'
}
};
const { johnDoe: { age, email }} = user;
const [a, b] = [1, 2, 3, 4, 5, 6];
console.log(a, b); // 1, 2
const [a, b,,, c] = [1, 2, 3, 4, 5, 6];
let a = 8, b = 6;
[a, b] = [b, a];
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b); // 1, 2
console.log(arr); // [3, 4, 5, 7]
const profileUpdate = ({ name, age, nationality, location }) => {
/* do something with these fields */
}
const getMousePosition = (x, y) => ({ x, y }); // returns { x: x, y: y }
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
var SpaceShuttle = function(targetPlanet){
this.targetPlanet = targetPlanet;
}
var zeus = new SpaceShuttle('Jupiter');
class SpaceShuttle {
constructor(targetPlanet) {
this.targetPlanet = targetPlanet;
}
}
const zeus = new SpaceShuttle('Jupiter');
A function.
class Book {
constructor(author) {
this._author = author;
}
// getter
get writer() {
return this._author;
}
// setter
set writer(updatedAuthor) {
this._author = updatedAuthor;
}
}
const lol = new Book('anonymous');
console.log(lol.writer); // anonymous
lol.writer = 'wut';
console.log(lol.writer); // wut
this._author = author;
<script type="module" src="filename.js"></script>
export const add = (x, y) => {
return x + y;
}
export { add, subtract };
import { add, subtract } from './math_functions.js';
import * as myMathModule from "./math_functions.js";
export default function add(x, y) {
return x + y;
}
import add from "./math_functions.js";
const makeServerRequest = new Promise((resolve, reject) => {
});
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer represents a response from a server
let responseFromServer;
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
- pending
- fulfilled
- rejected
myPromise.then(result => {
// do something with the result.
});
myPromise.catch(result => {
// do something with the result.
});