JavaScript Blogs 2
Data Types, Error handling , ES6
Data Types :
There are different types of data in JavaScript. They are-
- Number,
- String,
- Boolean,
- Undefined,
- Null,
- Objects,
- Function
Example :
typeof(2) //Number
typeof('1') //string
typeof (true) //boolean
typeof (undefined) //undefined
typeof(null) //null
typeof({}) //objects
typeof(index => index * value) //function
Error Handling :
Every programmer face error to his programming code. That is every day routing. Running code without error is a fantasy for all programmer . But there is a way to handle error in this code. For this to use catch in code.
Example :
try {
//programming code..
}catch(error){
//error handle
}
First try{…} code is executed, if catch error in code, stop first executed code and catch error executed .
Following this flowchart of the try…catch statement:
ES6 — Block Bindings
ES6 is acronym to ECMAScript6. It’s like updated JavaScript. It introduce some new things to us, tis we can use in JS code. Variable is more formally know as binding. We actually bind a value inside a scope. Scope refer parts of program.
Example :
var x;
let y;
const last = z;
variable Declarations
ES6 offers some best approaches for block bindings using var, let and const. But there are some things that JavaScript engine does behind the scene. Use a let & const variable before declared will result. I would go with variable binding:
Example :
//var
x = 2; // Assign value
var x; //declare x//let
let a; //declare a
a = 3;// Assign value //const
const b = 6 //declare & assign value
Arrow Function :
ES6 introduced arrow function to us. It is use first variable declared and function parameter then arrow sine and return function body. For this out put call this function. Down this example
Example
const add = (a, b) => return (a + b); //function declared
add(5 , 7) //function call
// output 12
Default parameter function
ES6 allowed default parameter value. Before declared a parameter and after assign value.
Example
function (a, b = 2) {
// function code
}
Spread operator
Spread operator allows an iterable to expands in places where at list one arguments are expected. It is used in variable array where there is more then one values expected.
Syntax : const variableName = […value];
Example
// spread operator doing the concat worklet array = [1,2,3];let array2 = [4,5];array = [...array1,...array2];console.log(array); // [ 1, 2, 3, 4, 5 ]
LOOPS
Loops are handy, if you want to run the same code over and over again, each time with a different value.
Example
for ( let i = 0; i < 10; i++) {
codes...
}
Comments
Write comments in the code is the best practice. Because any programmer seen comments know what is work tis code. Comments write two side bad comments and good comments.
Example
/**
* Returns x raised to the n-th power.
* @param {number} x The number to raise.
* @param {number} n The power, must be a natural number.
* @return {number} x raised to the n-th power.
*/
function power(x, n) {
...
}
Coding Style
Write clean and understable code is very important. Because one programmer code understand easily . If your code other programmer don’t understand, you will lose value to others. Down this example how to code clean …
Example
if (condition) {
// do this
// ...and that
// ...and that
}