rk reza
3 min readMay 6, 2021

--

JavaScript Error Handling

Error Handling:

Different types of errors during JavaScript code execution.

How to create JavaScript try and catch statements:-

Try & catch:

try {

// code…

} catch (err) {

// error handling

}

The try statement during execution helps to test a block of code for errors.

If an error in the try block, then the catch statement help to execution a block of code:

Example:

try {

alert(“hello Bangladesh”)

alert(Bangladesh is beautiful)

alert(“hello Bangladesh”)

} catch (error) {

alert(‘error massage’,error)

}

2. Block Bindings

Two more new keywords are coming to ES6, let and const.

The first of two new keyword we can say is a let like var But const is a different.

var keyword Example:

var name = ‘rkreza’;

console.log(‘My name is: ‘ + name);

name = ‘rezaul Karim’;

console.log(‘My name is: ‘ + name);

output:

My name is: rkreza

My name is: rezaul karim

Let keyword Example:

let name = ‘rkreza’;

console.log(‘My name is: ‘ + name);

name = ‘rezaul Karim’;

console.log(‘My name is: ‘ + name);

output:

My name is: rkreza

My name is: rezaul karim

Const:

Const means constant, whose value cannot be changed. You declare something with const you can’t change it after that.

Example:

const name = ‘rkreza’;

console.log(‘My name is: ‘ + name);

output:

My name is: rkreza

3.Funtion

A JavaScript function is a set of code that is created to perform a specific task.

To create a JavaScript function, the first write function keyword, then the name of function and right side () and the right side Curly brace {}.

Example:

function display() {

console.log(“Hello Bangladesh!”);

}

display();

output: Hello Bangladesh

4. Spread Operator

The spread operator basically spreads the array and the object.

Example:

We have an array of number

Let number = [22,33,44,55,66,77,88];

Spread operator apply

Let newNumber = […number,45,25,35,65,75,85];

5. Types of Values

JavaScript Supports six types of Data Types.

I.Boolean

II.Number

III.String

IV.Null

V.Undefined

VI.Symbol

I.Boolean

This data type only two types of value, true and false

Example:

Boolean (1)

True

Boolean(null);

False

II.Number

When a Data represents only Number, those Data is called Number Data Type in JavaScript.

data types: numbers, strings, objects and more:

Example:

let number = 16; // Number
let lastName = “rezaul”; // String
let name = {firstName:”rezaul”, lastName:”karim”}; // Object

III.String

When a Data represents only String, that is, a series of certain characters.

Example:

let lastName = “rezaul”; // String

V.Undefined

when a variable is declared but nothing is assigned or initialized. The only value of this data type is undefined.

Example:

function test(data) {

if (data === undefined) {

return ‘Undefined value!’;

}

return data;

}

let x;

console.log(test(x));

VI.Symbol

This gives each data a unique key

Example:

document.write(Symbol(‘dummy’) === Symbol(‘dummy’));

document.write(“<br>”);

document.write(‘dummy’===’dummy’);

6. Block-Level Functions:

A block statement is used to group zero or more statements.

Example:

alert(function(){

‘use strict’;

function f() { return 1; }

{

function f() { return 2; }

}

return f() === 1;

}());

6. Arrow Functions

If the function has only one statement, and the statement returns a value:

Example:

let display = () => {

return “Hello World!”;

}

7.anonymous function

Aanonymous function is a function without a name.

Example:

(function () {

console.log(‘Bangladesh’);

})

8. join

Connects all elements of an array to a string.

Example:

var laptop = [“dell”, “lenovo”, “Apple”, “Hp”];

laptop.join();

console.log(laptop)

9.pop():

JavaScript uses the pop () method to remove or delete a value from the end of an array.

Example:

var laptop = [“dell”, “lenovo”, “Apple”, “Hp”];

laptop.pop();

10. shift ()

JavaScript uses the shift () method to remove a value from the start array.

Example:

var laptop = [“dell”, “lenovo”, “Apple”, “Hp”];

laptop.shift();

console.log(laptop)

--

--