Enrich Your JavaScript Knowledge
1. Primitive Data Type: In JavaScript there are two kind of data types. one is primitive data types and another is non primitive data types.
The primitive data types in JavaScript are-
1. Number
2. String
3. Symbol
4. Undefined
5. Null
6. Boolean
2. Non Primitive Data types are Object and function.
3. setTimeout is a method of JavaScript which run the code which is written inside it after the minimum set time have finished.
Ex: setTimeout(()=>{
console.log(‘I will appear after 3s’);
},3000);
This doesn’t mean that the code will run after exactly 3 sec. It means the code will run after minimum 3 sec.
4. Try-catch-finally: When we write a code it might be wrong. there are many reasons our code might be wrong for that. When there is a chance to get an error in
our code or it it’s a complex code we should use try catch block.
The try statement allows us to define some code to be tested for errors. The try block will execute first. If there is no error then it will be executed
successfully. but if there are any error then it will go to the catch block and execute the code of the catch block. Now the application will not get crashed
because of using the catch block. There are another block called finally. After executing the try and catch block, Finally block will be executed. And it will
be definitely executed if error occurs or not.
5. Hoisting: When we run our JavaScript code it compiles the whole code. Hoisting is a process of JavaScript where variables and function declarations are moved to the
top of their scope before the code execution. Let’s see an example-
console.log(x); //(Output- “undefined”)
var x = 10;
Here is a confusing thing. We didn't get any error though we wanted to access a variable before initializing it. Its happening because of hoisting.
Before executing the whole code JavaScript take all the variables and functions and move them to the top of their scope and if it’s with a var keyword then immediately assign a value undefined with it. The code will work Like it-
var x = undefined;
console.log(x);
x = 10;
But when we use let, JavaScript moves it to the top but do not assign undefined immediately, rather it assign undefined to the line where the variable is declared. Like that-
console.log(z); //(output- reference error)
let z = 20;
Now think, why we getting the error??
Let’s see how it is working-
let z;
console.log(z) //(we can’t access a variable before initialization);
z = undefined;
z = 20;
6. scope: scope means a context of the code which determines the accessibilities of variables to JavaScript. In simple word, I will say scope is a separate
world of the code. There are two type of scopes. Local and Global. If we declare a variable in its global scope. then it can be accessible from anywhere
but it we declare a variable in a local scope then it can be accessible from its own function scope. but if we declare a variable with let in a global
scope then it can be accessible from only its block scope. Let’s see the example-
var x = 10;
function test(){
var y = 20;
console.log(x) //(output 10) //it can access its parent variable for the scope.
console.log(y) //(output 20)
}
console.log(x) //(output 10)
console.log(y) //(output -error) // it can not be accessible outside of it’s block scope bcoz it’s a local scope.
7. Function with default parameter value: Let’s see an example first-
function test1(text){
console.log(text)
}
test1(); //(output- undefined) because we didn’t pass any arguments.
we can handle this by setting a default parameter. Like that-
function test2(text = “Hello”){
console.log(text)
}
test2(); //(output- Hello) because we have set a default value;
8. Spread operator: The syntax of spread operator is (…) three dots. It allows an iterable and expands it into individual elements. It is mostly used in-
1. Copying an array.
2. Concatenating or combining arrays.
3. Using Math functions.
4. Using an array as arguments.
5. Adding an item to a list.
6. Adding to state in React.
7. Combining objects.
8. Converting Node List to an array.
9. Arrow function: Arrow function is introduced in es6. It allows us to create functions in simple and cleaner way compared to regular functions.
Ex: regular function-
const square= function(x) {
return x*x;
}
same function by arrow looks like-
const square = (x) => x*x;
its much simple than the previous one.
10. Coding style: we should maintain a standard coding style always. To be a good developer we have to keep our code clean and organized. I have created
a short list of standard coding styles. Here it is-
1. A space between parameters
2. Curly brace start {} on the same line, after a space
3. Spaces around operators
4. A semicolon ; is mandatory
5. Lines are not very long
6. use of } else { without a line break
7. Spaces around a nested call
8. An empty line between logical blocks
9. A space after for/if/while…
10. Indentation 2 spaces
11. No space between the function name and parentheses between the parentheses and the parameter