Javascript another Ten Trips

01|Function Default Parameter:

function add(num1, num2){
num2 = num2 || 5;
return num1 * num2;
}
const total = add(4)
//output is 20
function add(num1, num2 = 5){
return num1 * num2;
}
const total = add(4)
//output is 20
  • add function has two parameters. But users give one parameter.
  • Then it inter the function and see the other default parameter (num2= num2 || 5).
  • At last, the output is (4*5) 20.
  • The second function is similar to the first function. Now I am using the default value as using with parameter.

02| Spread Operator:

const number1 = [1,2,3,4];
const number2 = [6,7,8,9];
const totalNumber = [...number1, ...number2]
console.log(totalNumber)
//output is [1,2,3,4,6,7,8,9]
  • This is similar to concatmethod.
  • But using triple-dot that is coming ECMAScript 6 Model
  • It made easier programmer life.
  • You can easily concatenation one array to another array or any value.
  • It also works in Object.

03| Array Function:

01| x => x * x;
02| (x,y) => {
return x * y
}
  • Array Function is coming ECMAScript 6 Model and made easier of Developer Life.
  • The first array function x is a parameter and return x * x.
  • And The second function x and y is a parameter and return x * y
  • If you using one parameter you can’t use the second bracket.(Ex: 01)
  • But if you using two-parameter you using the second bracket for return (Ex: 02)
  • It is similar to the normal function

04| Var Declaration:

function any(){
var declaration = "I am Programmer"
if(true){
var declarationAgain = "I am JavaScript Programmer";
console.log(declaration);
console.log(declarationAgain);
}
console.log(declarationAgain);
}
any()
  • This is a function where using the var element. The first var element is executed in any place of “any” function. But you cannot execute the first var element outside the function. Because It is the local scope element.
  • The second var element is the same behavior as the first var element.
function any(){
var declaration = "I am Programmer"
if(true){
Let declarationAgain = "I am JavaScript Programmer";
console.log(declaration);
console.log(declarationAgain);
}
console.log(declarationAgain);
}
any()
  • But If using the let element in the “if” condition then it can’t execute any function in this function.
  • It is only executed inside if condition. If you want to execute outside the “if” condition it sees “syntax error”.

05| Template strings:

console.log('I am a programmer 1\n' +
'Javascript is the best programming language')
console.log(`I am a programmer
Javascript is the best programming language`)
  • Template strings are the new feature of ES6 and made easier for programmers.
  • Inside template strings, you can write any things. An example is the second console.log(). The symbol of template string is ``
  • Before coming, ES6 programmers can write text inside this ‘ ’ symbol and use 1\n for going the second line.
  • But using template string you can write an easily first, second, third, etc line.

06| Block-Level Declarations VS Function Declarations:

function any(){
var declaration = "I am Programmer"
if(true){
Let declarationAgain = "I am JavaScript Programmer";
console.log(declaration);
console.log(declarationAgain);
}
console.log(declarationAgain);
}
any()
  • Function Declaration is a declaration that executed in a function and declared by a “var” keyword.
  • “declaration” is executed at any place in the function.
  • But Block-Level Declaration is a declaration that executed in a specific place such as This example is using if condition.
  • “declarationAgain” is not executed outside the “if” condition.
  • Block-level declaration declared by “Let” keyword

07| Try … Catch blogs:

  • Try catch blogs is a very important thing for a developer.
  • All developers use this try …catch method.
  • If any error on your code that catch by try…catch method.
  • If the code has no error then the code goes to the try method and an error occurred in the code then it goes catch method.

08| try…catch…finally:

try {
alert( 'try' );
if (get a error){
excuted in catch block
}
}
catch (e) {
alert( 'catch' );
}
finally {
alert( 'finally' );
}
  • This is similar to try…catch.
  • But it is an extra thing that is finally.
  • “if” condition gets an error then it takes catch blog. Then finally blog executed.
  • If there is no error then execute try and finally block

09| Cross-Browser:

  • Cross-browser testing is a very important thing that the web sites and web apps you create work across an acceptable number of web browsers.
  • Cross Browser check is a very important thing for a developer.
  • When you planned to make a web app, you noticed the cross-browser.
  • After making the app you must be checked some default browser and some small-screened mobile or tablet that the apps running good.
  • At least you do three things for making an application that is planning, developing, texting with cross-browser.

10| Best Practice of written Function:

let multiply = any();
function(x,y) {
return x * y
}
  • This is the best practice written a function.
  • At first called a function then write the function details
  • After writing a function parameter give space before second brackets. It is a good practice.