From 7572f24835ff5785d57efe1e56a80a8f51e1e6cb Mon Sep 17 00:00:00 2001 From: Yuriy Degtyar Date: Fri, 25 Oct 2019 21:38:52 +0300 Subject: [PATCH] replace variable declarations with var to modern let and const --- JavaScript_Advance/callback.js | 2 +- JavaScript_Advance/cookies.js | 2 +- JavaScript_Advance/do_while_loop.js | 2 +- JavaScript_Advance/while_loop.js | 2 +- JavaScript_Basics/arrays.js | 10 +++++----- JavaScript_Basics/bugs.js | 4 ++-- JavaScript_Basics/exercise_1_using_arrays.js | 2 +- JavaScript_Basics/filter.js | 2 +- JavaScript_Basics/iife.js | 2 +- JavaScript_Basics/map.js | 2 +- JavaScript_Basics/reduce.js | 9 ++++----- JavaScript_Basics/strict_mode.js | 10 +++++----- JavaScript_Basics/this.js | 4 ++-- 13 files changed, 26 insertions(+), 27 deletions(-) diff --git a/JavaScript_Advance/callback.js b/JavaScript_Advance/callback.js index e2dec33..c428a93 100644 --- a/JavaScript_Advance/callback.js +++ b/JavaScript_Advance/callback.js @@ -62,7 +62,7 @@ transformNumber(10, (num) => { * passed to the `resolve()` function, will get passed to the function passed to `.then()` * function. Look at the following example from MDN official documents: */ -var p1 = new Promise((resolve, reject) => { +const p1 = new Promise((resolve, reject) => { resolve("Success!"); // or // reject(new Error("Error!")); diff --git a/JavaScript_Advance/cookies.js b/JavaScript_Advance/cookies.js index 17c3a63..708f235 100644 --- a/JavaScript_Advance/cookies.js +++ b/JavaScript_Advance/cookies.js @@ -6,7 +6,7 @@ console.log("Simple usage:" + document.cookie); // #2 Get a cookie with the name 'actor' document.cookie = "movie=Jungle Book"; document.cookie = "actor=Balu"; -var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)actor\s*\=\s*([^;]*).*$)|^.*$/, "$1"); +const cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)actor\s*\=\s*([^;]*).*$)|^.*$/, "$1"); console.log("Cookie with the name 'actor':" + cookieValue); // #3 Set cookie and execute code only once if cookie doesn't exist yet diff --git a/JavaScript_Advance/do_while_loop.js b/JavaScript_Advance/do_while_loop.js index fe4a8b9..ca82206 100644 --- a/JavaScript_Advance/do_while_loop.js +++ b/JavaScript_Advance/do_while_loop.js @@ -10,7 +10,7 @@ do { */ -var n = 1; +let n = 1; do { console.log("n is less than 6. n = " + n); diff --git a/JavaScript_Advance/while_loop.js b/JavaScript_Advance/while_loop.js index ac99790..c6f461f 100644 --- a/JavaScript_Advance/while_loop.js +++ b/JavaScript_Advance/while_loop.js @@ -10,7 +10,7 @@ */ -var n = 1; +let n = 1; while (n < 6) { console.log("n is less than 6. n = " + n); diff --git a/JavaScript_Basics/arrays.js b/JavaScript_Basics/arrays.js index 05cc3fb..1657417 100644 --- a/JavaScript_Basics/arrays.js +++ b/JavaScript_Basics/arrays.js @@ -27,26 +27,26 @@ console.log(easyMethod); // Output [ 'Swapnil Satish Shinde', 76, true ] // The pop() method removes the last element from an array: -var fruits = ["Banana", "Orange", "Apple", "Mango"]; +const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); // Removes the last element ("Mango") from fruits and output is ["Banana","Orange","Apple"] // Shifting is equivalent to popping, working on the first element instead of the last. // The shift() method removes the first array element and "shifts" all other elements to a lower index. -var cars = ["Acura", "Audi", "Bugatti", "Honda"]; +const cars = ["Acura", "Audi", "Bugatti", "Honda"]; cars.shift(); // Removes the first element ("Acura") from cars and output is ["Audi","Bugatti","Honda"] // The length property provides an easy way to append a new element to an array: -var mobiles = ["Apple", "Nokia", "Samsung", "Sony"]; +const mobiles = ["Apple", "Nokia", "Samsung", "Sony"]; mobiles[mobiles.length] = "HTC"; // Appends "HTC" to mobiles and output is ["Apple", "Nokia", "Samsung", "Sony", "HTC"] // delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined: -var myArray = ["a", "b", "c", "d"]; +const myArray = ["a", "b", "c", "d"]; delete myArray[0]; "The first value is: " + myArray[0]; // The first value is: undefined // Using delete may leave undefined holes in the array. Use pop() or shift() instead. // The splice() method removes items from array and/or adds items to array and returns the removed items: -var names = ["Anne", "Belle", "Chloe", "Diane", "Ella", "Frances"]; +const names = ["Anne", "Belle", "Chloe", "Diane", "Ella", "Frances"]; names.splice(4, 1); // This removes 1 item from index 4 // returns "Ella" diff --git a/JavaScript_Basics/bugs.js b/JavaScript_Basics/bugs.js index 18da994..2a428c2 100644 --- a/JavaScript_Basics/bugs.js +++ b/JavaScript_Basics/bugs.js @@ -1,8 +1,8 @@ -var obj = { +const obj = { name: "Some name" }; -var name = null; +const name = null; console.log(typeof obj); // object; console.log(typeof name); // object; \ No newline at end of file diff --git a/JavaScript_Basics/exercise_1_using_arrays.js b/JavaScript_Basics/exercise_1_using_arrays.js index 99d8c9e..f7788fd 100644 --- a/JavaScript_Basics/exercise_1_using_arrays.js +++ b/JavaScript_Basics/exercise_1_using_arrays.js @@ -1,4 +1,4 @@ -var array = [1, 0.16, "Random", function () { +const array = [1, 0.16, "Random", function () { console.log("this is function in an array"); }, { Key: "Answer" }, false]; diff --git a/JavaScript_Basics/filter.js b/JavaScript_Basics/filter.js index 9bdb4e8..166f18b 100644 --- a/JavaScript_Basics/filter.js +++ b/JavaScript_Basics/filter.js @@ -21,5 +21,5 @@ function isBigEnough (value) { return value >= 10; } -var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); +const filtered = [12, 5, 8, 130, 44].filter(isBigEnough); // filtered is [12, 130, 44] \ No newline at end of file diff --git a/JavaScript_Basics/iife.js b/JavaScript_Basics/iife.js index c9fcb7d..c8a01fd 100644 --- a/JavaScript_Basics/iife.js +++ b/JavaScript_Basics/iife.js @@ -7,6 +7,6 @@ Can be used as shown below:-- */ (function (value) { - var modified = value + 4; + const modified = value + 4; console.log(modified); }(3)); \ No newline at end of file diff --git a/JavaScript_Basics/map.js b/JavaScript_Basics/map.js index 8c933cb..fcdfb83 100644 --- a/JavaScript_Basics/map.js +++ b/JavaScript_Basics/map.js @@ -1,5 +1,5 @@ // Let's create an array of numbers that we want to get the square of each number in the array -var numbers = [1, 2, 3, 4, 5]; +const numbers = [1, 2, 3, 4, 5]; // pass a function to map const square = numbers.map(function (num) { diff --git a/JavaScript_Basics/reduce.js b/JavaScript_Basics/reduce.js index 84ca2b8..702f4b5 100644 --- a/JavaScript_Basics/reduce.js +++ b/JavaScript_Basics/reduce.js @@ -5,11 +5,10 @@ the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. */ -var pokemon = ["squirtle", "charmander", "bulbasaur"]; +const pokemon = ["squirtle", "charmander", "bulbasaur"]; -var pokeLength = - pokemon.reduce(function (previous, current) { - return previous + current.length; - }, 0); +const pokeLength = pokemon.reduce(function (previous, current) { + return previous + current.length; +}, 0); // Outputs 27 \ No newline at end of file diff --git a/JavaScript_Basics/strict_mode.js b/JavaScript_Basics/strict_mode.js index ad1b060..8eca404 100644 --- a/JavaScript_Basics/strict_mode.js +++ b/JavaScript_Basics/strict_mode.js @@ -2,20 +2,20 @@ // examples taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode // Assignment to a non-writable global -var undefined = 5; // throws a TypeError -var Infinity = 5; // throws a TypeError +const undefined = 5; // throws a TypeError +const Infinity = 5; // throws a TypeError // Assignment to a non-writable property -var obj1 = {}; +const obj1 = {}; Object.defineProperty(obj1, "x", { value: 42, writable: false }); obj1.x = 9; // throws a TypeError // Assignment to a getter-only property -var obj2 = { get x () { return 17; } }; +const obj2 = { get x () { return 17; } }; obj2.x = 5; // throws a TypeError // Assignment to a new property on a non-extensible object -var fixed = {}; +const fixed = {}; Object.preventExtensions(fixed); fixed.newProp = "ohai"; // throws a TypeError diff --git a/JavaScript_Basics/this.js b/JavaScript_Basics/this.js index 208ed19..edd549c 100644 --- a/JavaScript_Basics/this.js +++ b/JavaScript_Basics/this.js @@ -1,7 +1,7 @@ console.log(this); // This gives empty object // Output {} -var aa = 1; +const aa = 1; console.log(global.aa); // Global scope is accessible to every where // Output undefined @@ -19,7 +19,7 @@ console.log(a); // console.log(b) // Output ReferenceError: b is not defined as b is defined using let it is going to be declared only in that block const help = () => { - var a = 4; + const a = 4; const b = 2; // variables defined by let and const are accessible to there scope only console.log(a); // This will not get printed unless and until function is called };