DEV Community

Diganta165
Diganta165

Posted on

Ten Interesting Concepts of JavaScript

1)
Primitive:
A primitive is data that has is not an object and has no methods. All the primitives are immutable which means that they can’t be altered. There are seven primitive data types-
i. string
ii. number
iii. bigint
iv. Boolean
v. Undefined
vi. Symbol
vii. Null

2)
Value Type:
JavaScript is not like other languages where you have to declare value type. So how do you know what value falls into what category? In general there are nine value types. Seven primitive values and objects and functions. But there is a way to know the type of a variable. That is typeof() function.
Console.log(typeof(2));
//it will show the type of value 2 which is number

3)
Error Handling (try…catch):
It is a common thing when we try to run a code and some error occurs. When an error occurs the script just dies. So when an error occurs instead of dying we can make the script do something reasonable with try…catch
The try…catch construct has two main blocks: try and catch,
Try{
//code…
} catch(err){
//error handling
}
Here the main code is in try block. If there is no errors then the catch(err) block will be ignored. But if an error occurs the try execution will be stopped and the catch block will start to execute. The err variable will contain an error object with details about what happened.

4)
Cross Browser Testing:
Cross browser testing is the practice of making sure that the web site and web app is supported through an acceptable amount of web browsers. It is a developer’s responsibility to make sure that not only to make apps but also to check if it works well at the user end no matter what browser, device or any tool they are using.

5)
Comments:
Comments can be both single lined and multi lined. In JS single line comments are written starting with // and multiline //
But comment should be minimal in coding. It is a good practice to write code in a way that you don’t need to explain elaborately with comments.

6)
Default Parameter:
If we call a function with a missing argument then missing parameter value is set to undefined. So what we can do is we can set a value to the parameter which will be default and it will work only if we don’t pass the argument value.

function function_name(x, y=4){
//function code
}
7)
Spread Operator:
Spread Operator is a variable starting with three dots(…). It is normally used when you need to add a element into an array or list. What it does is it copies all the before elements of an array/list then in the end add the new element.
8)
Arrow Function:
Arrow functions were introduced in ES6. It allows us to create function in a cleaner way.
//normal function
const x = function(x, y) {
return x+y;
}

//arrow function
const x = (x,y) => x+y;
Arrow function is situational. If a function has only one argument then we can omit the parentheses.
const pay = x => console.log(x);

9)
Hoisting:
In hoisting the variables and function declarations are put into memory during the compile phase but stay exactly where we type them in the code. Hoisting allow us to use a function before we declare it into the code.

10)
Block Binding in Loops:
Block binding is very useful when using loops. It is best practice to use let instead of var because var is being hoisted.
for(var i = 0; i< 10; i++){
//code
}
In this case i is accessible from the outside of the for loop because we have declared it using var. But in the case of let we wouldn’t be able to access it outside of the loop.

Top comments (0)