Tricky Javascript
1. Check the datatype of your variable :
You can use typeof( ) function to check the datatype of your variable.
Example :
let x =10;
let name = “Niloy”
console.log(typeof(x)); // Output : number
console.log(typeof(name)); // Output : string
2. Use of try catch:
It has mainly two blocks. One is try {…} and other one is catch{….}. See the code below :
try { // code… }
catch (err) { // error handling }
- First, the code in
try {...}
is executed. - If there is no error in the try block, catch block is ignored.
- If there is any error in the try block, then the
try
execution is stopped, and control flows to the beginning ofcatch(err)
. Theerr
variable (we can use any name for it) will contain an error object with details about what happened.
3. Use of try…catch…finally:
The syntax is something like this :
try { … try to execute the code … }
catch(e) { … handle errors … }
finally { … execute always … }
This finally block will be executed always after the try catch block executed.
If it exists, it runs in all cases:
- after
try
, if there were no errors, - after
catch
, if there were errors.
4. Use of try…finally:
The try..finally
block, without catch
block, is also useful. We apply it when we don’t want to handle errors in the code, but want to be sure that processes that we started are finalized.
Syntax :
try { // … }
finally { // complete that thing even if all dies }
5. Cross Browser Testing :
It is basically the practice of making sure that the web sites and web apps you create work across an acceptable number of web browsers. As a web developer, it is your responsibility to make sure that not only do your projects work, but they work for all your users, no matter what browser, device, or additional assistive tools they are using.
6. Javascript ES6 — Arrow Fucntion :
It gives a cleaner look to your declared function compared to the previous javascript.
General Function :
function hello(){
console.log(“Hello World”)
}
Arrow Function :
const hello = () => console.log(“Hello World”);
Both function returns the same output, but arrow function looks more cleaner.
7. Javascript ES6 — Spread Operator:
Spread operator simply means three dots (…) . It is used to expand an iterable object into the list of arguments.
Example :
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(…numbers)); //output: 6
What else can be done with Spread Operator :
- Copying an array
- Concatenating or combining arrays
- Using an array as arguments
- Adding an item to a list
- Adding to state in React
8. Javascript — Default Parameter:
Default function parameters allow named parameters to be initialized with default values if no value or undefined
is passed.
Code :
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5, 2));
// expected output: 10
console.log(multiply(5));
// expected output: 5
9. Javascript — Scope:
Scope determines the visibility or accessibility of a variable or other resource in the area of your code.
Global Scope :
There’s only one Global scope in the JavaScript document. The area outside all the functions is consider the global scope and the variables defined inside the global scope can be accessed in any other scopes.
Code :
//global scope
var fruit = 'apple'
console.log(fruit); //apple
function getFruit(){
console.log(fruit); //fruit is accessible here
}
getFruit(); //apple
Function Scope :
Whenever you declare a variable in a function, the variable is visible only within the function. You can’t access it outside the function. var is the keyword to define variable for a function-scope accessibility.
Code :
function foo(){
var fruit ='apple';
console.log('inside function: ',fruit);
}
foo(); //inside function: apple
console.log(fruit); //error: fruit is not defined
10. Javascript — Binding:
Variable is more formally known as binding. When we declare and/or initialize a variable, we actually bind a value to a name inside a scope. Scope usually refers to a specific part of a program.
So binding occurs whenever we declare and/or initialize a variable using one of these keywords: var, let and const in JavaScript.