Some Javascript Basic Concept

Primitive Data Type:

There are seven primitive data types. These are string, number, boolean, null, undefined, Symbols, bigInts.

Example:

console.log(typeOf(“My book”))

Output will be string

console.log(typeOf(23))

Output will be number

console.log(typeOf(22%2==0))

Output will be true which is a boolean value

Others types:

Other types are objects and function. Array consider as the object type. Similarly, date and regular expressions are also considered as object data types.

Example

console.log(typeOf({name:’sajid’}))

Output will be object

console.log(typeOf([1,2,3,4]))

This output also object

console.log(typeOf(m=>m*4))

Output is function

try and catch (error handling)

Sometimes unfortunately some mistakes are made by programmers. If those mistakes are not in syntax javascript gives an option to solve them. try and catch.

Example

try{

alert(‘successfully run’);

}

catch(err){

alert(‘your code is some error’);

}

Let's explain how its work

At first try block execute and if there were no error catch blocks do not work. If any error finds in the code, try execution doesn’t work this time and goes to catch block and show the error.

Cross-browser testing:

As a developer, everyone should know about browser testing. If your website is not working on all the browsers and devices, it will create a bad impact on your development carrier. Before publishing your website, you need to test your website in a different browser (like chrome, firefox, safari, etc). Also, you should test it on mobile and ios.

It is very painful to test large projects. There is a solution for it. Split your code into various sections. then test part by part. It will be helpful for you.

Block-level declaration:

Block-level declarations are one of the fundamental declaration techniques in ES6. In venilla Js, var is used in variable declaration. var is a global scoped. It can be called from anywhere. After coming to ES6, let and const have solved these issues. These twos are local scoped. If you declared any variable with let in any function block. This let will work only inside of this function.

const checkFunction=()=>{

let check=’hi’

console.log(check)

//output will be ‘hi’

}

console.log(check)

// output will be undefined

if you want to change your variable value you must work with let and for fixed value const is better for use.

Block Binding In loops:

Sometimes programmers make a mistake, they declare var inside the for loop iteration.

for(var i=0;i<10;i++)

var is a global scope and then it will work on the whole code. This is the problem. After complete iteration, the value of i is 9

And this problem solved by ES6. If we write this for loop with let then code will be

for(let i=0;i<10;i++)

When we write let it means it is a local scope and this i value only work with this loop block only. If we check the i value after the execution we will see undefined. It means it is not working outside.

Var declaration and hoisting:

var is called global scope. They are sometimes declared outside of the function. If actual declaration occurred outside of function this is called hoisting.

var check;

if(condition)

{

check=’i am in if block’

}

else{

}

In if block, initialization occurred but in else part this variable also can accessible. If we check output here we can find undefined. It happens because we declare the value is hoisted is the top.

Spread operator:

Spread operator targets some value and then copies, concates to another variable. The symbol of the spread operator is… (Three dots). This operator used in object and array.

const check=[‘a’,’b’,’c’]

const check2 = [‘d’,’e’,’f’]

const newCheck =[…check,…check2]

Output will be [‘a’,’b’,’c’,’d’,’e’,’f’]

Function with default paramater:

Suppose

function summation (a,b){

return a+b;

}

console.log(summation(5))

Its output will be undefined. There is a solution for it. If we write similar code

function summation (a,b=1){

return a+b;

}

console.log(summation(5))

Here we check the output will be 5+1=6

What happened there, In the second code we set a default value for the b parameter. This value is working only when the second argument is not set. function summation (a,b=1){

return a+b;

}

console.log(summation(5, 8))

Here we check the output will be 5+8=13

Default value not affect in the set argument.

Arrow function :

Arrow function is a javascript popular function expression. It is limited and easily usable for all situations.

function myFunc(a,b){

return a+b;

}

Traditional method there is extra parentheses used for one line code

but in arrow function

const myFunc=(a,b)=>return a+b;

I am a Mern Stack web developer.