- Var
- Let
- Block Scope "this"
- Const
- Hoisting
- Arrow Functions
- Default Parameters
- Object Literals
- Rest Operator
- Spread Operator
- For Of Loop
- Template Literals
- Destructuring
-
1_var.js
var name = 'MCA'; console.log(name); Output: 'MCA'
-
2_let.js
let name = 'MCA'; console.log(name); Output: 'MCA'
-
3_blockscope_error.js
'let' use block scope, the variable just exists in the "if" block.
if(true){ let age = 40; } console.log(age); Output: ReferenceError: name is not defined.
-
4_blockscope.js
age = 33; if(true){ let age = 40; } console.log(age); Output: 33
-
5_blockscope.js
let age = 33; if(true){ let age = 40; console.log(age); } console.log(age); Output: 40 33
-
6_blockscope.js
let age = 33; if(true){ let age = 40; console.log(age); } console.log(age); age = 29; console.log(age); Output: 40 33 29
-
With "let" be sure the variable you use inside of a block stay inside a block.
-
7_const.js
const age = 33; console.log(age); Output: 33
-
8_const_error.js
const age = 38; age = 33; console.log(age); Output: TypeError: Assignment to constant variable.
-
9_constant.js
const AGE = 38; console.log(AGE); Output: 38
-
10_constant_array.js
const AGES = [27,33,38]; console.log(AGES); Output: [27,33,38]
-
11_constant_array_push.js
const AGES = [27,33,38]; console.log(AGES); AGES.push(40); console.log(AGES); Output: [27,33,38] [27,33,38,40]
-
12_constant_obj.js
const OBJ = { age: 33 }; console.log(OBJ); OBJ.age = 40; console.log(OBJ); Output: { age: 33 } { age: 40 }
-
13_hoisting_var.js
age = 33; console.log(age); var age; Output: 33
-
14_hoisting_let_error.js
age = 33; console.log(age); let age; Output: ReferenceError: Cannot access 'age' before initialization at Object.
-
We have to declare it before initialize.
-
14_hoisting_let.js
let age; age = 33; console.log(age); Output: 33
-
16_hoisting_let_function.js
function doSmth(){ age = 33; } let age; doSmth(); console.log(age); Output: 33
- You have to declare things before act using then.
-
17_normal_function.js
function fn(){ console.log('Hello!'); } fn(); Output: 'Hello!'
-
18_arrow_function.js
var fn = () => { console.log('Hello!'); } fn(); Output: 'Hello!'
-
19_arrow_function_shorter.js
var fn = () => console.log('Hello!'); fn(); Output: 'Hello!'
-
20_arrow_function_much_shorter.js
var fn = () => 'Hello!'; console.log(fn()); Output: 'Hello!'
-
21_effect_as_the_same_as_example_before.js
function fn(){ return 'Hello!'; } console.log(fn()); Output: 'Hello!'
-
22_arrow_function_multiply.js
var fn = () => { let a = 2; let b = 3; return a * b; }; console.log(fn()); Output: 6
-
23_arrow_function_parameters.js
var fn = (a,b) => { return a * b; }; console.log(fn(2,3)); Output: 6
-
24_arrow_function_parameters_one_line.js
var fn = (a,b) => a * b; console.log(fn(2,3)); Output: 6
-
25_arrow_function_parameters_one_argument.js
var fn = (a) => a * 2; console.log(fn(2)); * If you pass a second argument, will be ignored. console.log(fn(2,3)); Output: 4 4
-
26_arrow_function_parameters_one_argument.js
-
When we have just one argument we can leave the paranteses
var fn = a => a * 2; console.log(fn(2)); Output: 4
-
27_arrow_function_settimeout.js
SetTimeOut(() => console.log('Hello!',1000)); Output: 'Hello!' after 1 second.
-
28_normal_function_this.js
function fn(){ console.log(this); } fn(); Output Terminal: <ref *1> Object [global] Output Console Browser: Window {0: global,..}
-
29_arrow_function_this.js
var fn = () => console.log(this); fn(); Output Terminal: {} Output Console Browser: Window {0: global,..} Here "this" refers to global scope.
-
30_normal_function_this_button.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Normal Function This Button</title> </head> <body> <button>Test</button> </body> </html> <script> var button = document.querySelector('button'); function fn(){ console.log(this); } button.addEventListener('click',fn); // Output: <button>Test</button> // Here "this" refers to button scope. </script>
-
31_arrow_function_this_button.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Arrow Function This Button</title> </head> <body> <button>Test</button> </body> </html> <script> var button = document.querySelector('button'); var fn = () => console.log(this); button.addEventListener('click',fn); /* * Output: <button>Test</button> * Here "this" refers to global scope. */ </script>
-
"this" are handleed diferent dependes the context.
-
The Fat Arrow Functions you wont´t need bind, apply or call all these workaround uses with ES5 to get "this" to the right context.
-
Just keeps the context no mather how or where you call this function.
-
32_normal_parameters.js
function isEqualTo(number,compare){ return number == compare; } console.log(isEqualTo(10,10)); Output: true
-
33_default_paramenters_false.js
function isEqualTo(number,compare = 0){ return number == compare; } console.log(isEqualTo(10)); Output: false
-
34_default_paramenters_true.js
function isEqualTo(number,compare = 10){ return number == compare; } console.log(isEqualTo(10)); Output: true
-
35_default_paramenters_both.js
function isEqualTo(number = 10,compare = 10){ return number == compare; } console.log(isEqualTo()); Output: true
-
36_default_paramenters_one.js
function isEqualTo(number = 10,compare){ return number == compare; } console.log(isEqualTo(11)); Output: false
-
37_default_paramenters_one_debug.js
function isEqualTo(number = 10,compare){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo(11)); Output: 11 undefined false
-
38_default_paramenters_skip_one.js
function isEqualTo(number,compare = 10){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo(11)); Output: 11 10 false
-
39_default_paramenters_calculation.js
function isEqualTo(number,compare = 10 / 2){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo(11)); Output: 11 5 false
-
40_default_paramenters_the_same.js
function isEqualTo(number,compare = number){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo(11)); Output: 11 11 true
-
41_default_paramenters_declared_outside.js
let a = 100; function isEqualTo(number,compare = a){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo(11)); Output: 11 11 true
-
42_default_paramenters_inverted.js
function isEqualTo(number = compare,compare = 10){ console.log(number); console.log(compare); return number == compare; } console.log(isEqualTo()); Output: ReferenceError: Cannot access 'compare' before initialization at isEqualTo
- The error is because the compare only will be created in the second step, in first step this not exists yet.
-
43_object_literal.js
let obj = { name: 'MCA', age: 33 }; console.log(obj); Output: { name: 'MCA', age: 33 }
-
44_object_literal_external_assigned.js
let name = 'MCA'; let age = 33; let obj = { name, age }; console.log(obj); Output: { name: 'MCA', age: 33 }
- The object will take the values from the surrounding variables. We not specify values to initialize the object. It automatic look if has variables declared before the declaration object that matches the propertie names.
-
45_object_literal_not_assigned.js
let obj = { name, age }; console.log(obj); Output: ReferenceError: name is not defined at Object.
- The object properties need to be initialized.
-
46_object_literal_external_assigned_override.js
let name = 'MCA'; let age = 33; let obj = { name: 'DIGITAL', age }; console.log(obj); Output: { name: 'DIGITAL', age: 33 }
- The object properties will be override.
-
47_object_literal_function_propertie.js
let name = 'MCA'; let age = 33; let obj = { name: 'DIGITAL', age, greet(){ console.log(this.name + ', ' + this.age); } }; obj.greet(); Output: DIGITAL, 33
-
48_object_literal_propertie_string.js
let name = 'MCA'; let age = 33; let obj = { "name": 'DIGITAL', age, "greet"(){ console.log(this.name + ', ' + this.age); }, "greet me"(){ console.log(this.name + ', ' + this.age); } }; obj["greet"](); obj["greet me"](); Output: DIGITAL, 33
-
49_object_literal_propertie_string_dinamic_error.js
let name = 'MCA'; let age = 33; let ageField = "age"; let obj = { "name": 'DIGITAL', [ageField], "greet me"(){ console.log(this.name + ', ' + this.age); } }; obj["greet me"](); Output: SyntaxError: Unexpected token ','
-
50_object_literal_propertie_string_dinamic.js
let name = 'MCA'; let age = 33; let ageField = "age"; let obj = { "name": 'DIGITAL', [ageField]: 40, "greet me"(){ console.log(this.name + ', ' + this.age); } }; obj["greet me"](); console.log(obj.age); console.log(obj[ageField]); console.log(obj["age"]); Output: DIGITAL, 40 40 40 40
- Rest operators return a array when passed as parameters.
-
51_the_array_parameter.js
function sumUp(toAdd){ let result = 0; for(let i=0; i<toAdd.length; i++){ result += toAdd[i]; } return result; } let numbers = [1,2,3,4,5]; console.log(sumUp(numbers)); Output: 15
-
52_the_rest_operator.js
function sumUp(...toAdd){ console.log(toAdd); let result = 0; for(let i=0; i<toAdd.length; i++){ result += toAdd[i]; } return result; } console.log(sumUp(100,10,20)); Output: 130
-
53_the_rest_operator_with_string.js
function sumUp(...toAdd){ console.log(toAdd); let result = 0; for(let i=0; i<toAdd.length; i++){ result += toAdd[i]; } return result; } console.log(sumUp(100,10,"20")); Output: "11020"
- Spread operator does the oposite of Rest Operator.
-
54_the_spread_operator.js
let numbers = [1,2,3,4,5]; console.log(...numbers); console.log(Math.max(numbers)); console.log(Math.max(...numbers)); Output: 1 2 3 4 5 NaN 5
-
55_the_for_of_loop.js
let testResults = [1.23,1.10,4.1]; for(testResult of testResults){ console.log(testResult); } Output: 1.23 1.1 4.1
-
56_template_literals.js
let name = 'MCA'; let description = ` Like this `; console.log(name); console.log(description); Output: MCA Like this
-
57_template_literals_variables.js
let name = 'MCA', date = new Date(), year = date.getFullYear(), month = date.getMonth(), day = date.getDate(); let description = ` Hello, \${name} we are in \${day+'/'+month+'/'+year} Hello, ${name} we are in ${day+'/'+month+'/'+year} `; console.log(description); Output: Hello, ${name} we are in ${day+'/'+month+'/'+year} Hello, MCA we are in 23/2/2021
-
58_normal_array.js
let numbers = [1,2,3]; let a = numbers[0]; let b = numbers[1]; console.log(numbers); console.log(a); console.log(b); Output: [ 1, 2, 3 ] 1 2
-
59_destructuring_array.js
let numbers = [1,2,3]; let [a,b] = numbers; console.log(a); console.log(b); console.log(numbers); Output: 1 2 [ 1, 2, 3 ]
-
60_destructuring_array_undefined.js
let numbers = [1,2,3]; let [a,b,c,d] = numbers; console.log(a); console.log(b); console.log(c); console.log(d); console.log(numbers); Output: 1 2 3 undefined [ 1, 2, 3 ]
-
61_destructuring_array_rest.js
let numbers = [1,2,3]; let [a,...b] = numbers; console.log(a); console.log(b); console.log(numbers); Output: 1 [ 2, 3 ] [ 1, 2, 3 ]
-
62_destructuring_array_rest_error.js
let numbers = [1,2,3]; let [a,...b,c] = numbers; console.log(a); console.log(b); console.log(c); console.log(numbers); Output: let [a,...b,c] = numbers; ^^^^ SyntaxError: Rest element must be last element.
-
63_destructuring_array_default.js
-
A mix of Default Values And Destructuring.
let numbers = [1,2,"3"]; let [a=0,b,c,d='Default'] = numbers;
console.log(a); console.log(b); console.log(c); console.log(d); console.log(numbers);
Output: 1 2 "3" "Default" [ 1, 2, 3 ]
-
-
64_swap_variables.js
let a = 5; let b = 10; let c = a; console.log("a: ",a); console.log("b: ",b); a = b; b = c; console.log("a: ",a); console.log("b: ",b); Output: a: 5 b: 10 a: 10 b: 5
-
65_destructuring_swap_variables.js
let a = 5; let b = 10; console.log("a: ",a); console.log("b: ",b); [b,a] = [a,b]; console.log("a: ",a); console.log("b: ",b); Output: a: 5 b: 10 a: 10 b: 5
-
66_destructuring_array_spaces.js
let numbers = [1,2,3]; let [a, ,c] = numbers; console.log(a,b); Output: 1 3
-
67_destructuring_create_variables.js
let [a,b] = [1,2,3]; console.log(a,b); Output: 1 2
-
68_destructuring_objects.js
let obj = { name: 'MCA', age: 33 }; let {name,age} = obj; console.log(name); console.log(age); Output: MCA 33
-
69_destructuring_objects_function.js
let obj = { name: 'MCA', age: 33, greet: function(){ console.log('Hello there!'); } }; let {name,greet} = obj; greet(); Output: "Hello there!"
-
70_destructuring_objects_function_alias.js
let obj = { name: 'MCA', age: 33, greet: function(){ console.log('Hello there!'); } }; let {name,greet: hello} = obj; hello(); greet();
-
If will try call greet() it will give a error.
-
Outside object just exists hello() function.
Output: "Hello there!" ReferenceError: greet is not defined.
-
- ES6 Compatibility Table
- Test javascript.
- ES6 JavaScript Tutorial for Beginners - Getting Started
- https://www.youtube.com/watch?v=IEf1KAcK6A8