Javascript Recap 2
Block Binding in loops
In a case where developers want block-level scooping of most variables and one of the ones used in counter variables.
The following is an example:
for (var i=0; i < 10; i++) {
process(items[i]);
}
// i is still accessible here
console.log(i);
In other languages, block-level scooters work by default. Axis should be out there just for people. The declaration is lifted after the man of the variable in JavaScript is finished.
for (let i=0; i < 10; i++) {
process(items[i]);
}// i is not accessible here — throws an error
console.log(i);
Global Block bindings
Another way is to let and const different var using a global scope. When var is used in the global environment, it creates a new global variable, which is a browser (window of browsers) of global objects.
var RegExp = “Hello!”;
console.log(window.RegExp);
var content= “Hi!”;
console.log(window.content);
Yet it is not safe to overwrite any declaration. This example announces a new global variable RegExp that overwrites the original. Similarly, NCZ is defined as a global variable
Emerging Best Practices for Block Bindings
During the development of ECMAScript 6, you believed that you should use var instead of var for variable declarations. For many widespread JavaScript developers, They behave exactly the way they should have behaved and so direct replacement makes sense. The argument is that most variables should not change their values after starting because unexpected value changes are the source of the bug. This concept has a significant amount of traction and your code is searchable as soon as you adopt ECMAScript 6.
Functions with Default Parameter Values
Functions in JavaScript are unique because they allow the passage of any parameter regardless of the number of parameters declared in the function definition. This allows you to define functions that can handle different parameters, usually by default, if parameters are not provided.
Working with Unnamed Parameters
JavaScript functions do not limit the number of parameters that can go into the number of defined named parameters. You can always pass more or less than the officially specified parameters. Specifies default parameter values when a function may accept fewer parameters, And ECMAScript sought tried to create the problem of passing more parameters than better defined.
Unnamed Parameters in ECMAScript 5
First, JavaScript provides arguments as a way to test all the function parameters passed without defining each parameter individually.
function pick(object) {
let result = Object.create(null);
// start at the second parameter
for (let i = 1, len = arguments.length; i < len; i++) {
result[arguments[i]] = object[arguments[i]];
}
return result;
}
let book = {
title: “Hero Lerner”,
author: “Hero Developer”,
year: 2021
};
let bookData = pick(book, “title” ,“author”, “year”);
console.log(bookData.title); // expected output:Hero Lerner
console.log(bookData.author); // expected output:Hero Developer
console.log(bookData.year); // expected output:2021
The Spread Operator
The spread operator is closely related to the rest parameters. The rest of the parameters allow you to specify whether you need to combine multiple individual arguments into one array, The spread operator lets you specify an array that should be split and its items should be sent to a function as a separate argument Math. max ().
let num1 = 22,
num2 = 15;
console.log(Math.max(num1, num2)); // expected output:22
When you are working with only two values, as in this example, Math.max () is very easy to use. Two values are passed, and higher values are returned.
The Math. max () method does not allow you to access arrays, so with ECMAScript 5 and earlier, you will be stuck manually searching for arrays or using the application ():
let number= [25, 50, 75, 100]
console.log(Math.max.apply(Math, number)); // expected output:100
Block-Level Functions
ECMAScript 3 and earlier, declaring an activity inside a block (a block-level function) was technically a syntax error, but all browsers still support it. Unfortunately, every browser that allows syntax to behave a little differently is considered a best practice to avoid announcing functions inside blocks (the best option is to use a function expression).
“use strict”;
if (true) {
// Throws a syntax error in ES5, not so in ES6
function doSomething() {
// …
}
}
In ECMAScript 5, this code throws a syntax error. In ECMAScript, the dismounting () function is treated as a block-level declaration.