24
loading...
This website collects cookies to deliver better user experience
const
in your JavaScript code for the variables and methods. This will help you in avoiding the unexpected change in the value or reassignment of the variables.const name = 'Delhi';
const getName = function(){
name = 'Mumbai'; // this will throw an error
return name;
}
getName();
const
won't let the accidental value update. There are hardly any programs that can be done using only const
. We need to declare the variables, & methods. let
but instead of making them global, we should make their scope as per the requirements. Why? global variables could be error-prone. declare global only when it is unavoidable and required.var a = 2;
var b = 3;
var result = a + b;
return result;
function sumNumbers(a,b){
return result = a + b;
}
sumNumbers(2,3);
map()
, reduce()
, filter()
, every()
, some()
. They (map()) implicitly return a new array. Do you know what it means? const a = [1,2,3,4]
let tempArr = [];
for(let i =0; i < a.length; i++){
tempArr.push(a[i]);
}
return tempArr;
const a = [1,2,3,4];
const b = a.map((val, index) => {
return val;
});
return b;
/* pass value to add numbers */
/**
* return the sum of the two given arguments
* @type numbers
*/
functions
. So, that it can be reuse, test once, avoid errors, save time, and have consistency.try/catch
to handle the error and exceptions. This is one thing that a lot of developers just missed (including me). Using try/catch is an important way to avoid the embarrassing errors messages on browsers.map()
is an example of the pure function but Math.random()
is not.