JavaScript Exercises
1) const variable must be initialized:
function test(){
const mydog = 'spotty' // good
mydog = 'fluffy' // error: assignment to constant
const mybrother; // error: Missing initializer in
const
}
2) While a const variable cannot be reassigned entirely to a different value, if the value of
a const is an object or array, the object’s properties themselves are still mutable, able to be modified:
function test(){
const myobject = {name:'George', age:39}
//myobject = {name: 'Ken', age:39} //error
myobject.age = 40 // OK
const myarray = []
myarray[0] = 'Football' // OK
}
3)Arrow function
- • JavaScript arrow functions are anonymous functions
- • Arrow functions cannot be used as constructor functions, (ie: with the keyword new)
- • Lexical binding of this inside arrow function: The value of this inside an arrow function
- always points to the same this object of the scope the function is defined in, and never
- changes.
4) Let Scope
function letDemo(){
let age=25;
if(new Date().getFullYear()==2017){
age=32;
console.log(age);
}
console.log(age);
}
Unlike var, you cannot define the same let variable more than once inside a block:
function letDemo(){
let myage = 39
let myname = 'Capgemini'
let myage = 40 // SyntaxError: Identifier myage has
already been declared
}
Note :this will show error myage already declared.
Let inside for loop.
function greet(){
alert('hello');
}
function letDemo(){
/*for (var i = 0; i < 5; i++){
setTimeout(function(){
console.log(i)
}, i * 100)
}
//logs '5, 5, 5, 5, 5'
*/
/*for (var i = 0; i < 5; i++){
(function(x){
setTimeout(function(){
console.log(x)
}, i * 100)
})(i)
}
//logs '0, 1, 2, 3, 4'
//this is IIFE inside
*/
for (let i = 0; i < 5; i++){
setTimeout(function(){
console.log(i)
}, i * 100)
}
//logs '0, 1, 2, 3, 4'
}
5) Prototype
The JavaScript prototype property allows you to add new properties to an existing prototype.
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
------------------------------------------------------------------------
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
6) Async Functions
Async functions are defined by placing the async keyword in front of a function, such as:
async fetchdata(url){
// Do something
// Always returns a promise
}
Async does two things:
- It causes the function to always return a promise whether or not you explicitly return something, so you can call then() on it for example. More on this later.
- It allows you to use the await keyword inside it to wait on a promise until it is resolved before continuing on to the next line inside the async function.
“Async functions always return a promise. If we explicitly return a value at the end of our async function, the promise will be resolved with that value; otherwise, it resolves with undefined.”
Closure Example:
function sayHello(name){
var text = 'Hello ' + name;
var sayAlert = function(){ alert(text); }
return sayAlert();
}
Pingback: Modern JavaScript Tutorial