10 JavaScript Concepts you must know as a JavaScript programmer.

JavaScript Concepts

Error Handling………

Overview :

Suppose, I did a coding of 100 lines. I have an error in line 3. In this case, if I use error handling, the program will not stop for of my mistake in line 3. The program will continue by skipping line 3. Remember that, it’s most important for us a programmer.

* Types of Errors

(i) Syntax Error: Error in the syntax. For example, if you write console.log(‘your result’), the above program throws a syntax error. The spelling of the console is a mistake in the above code.

(i) Runtime Error: This type of error occurs during the execution of the program. For example,
calling an invalid function or a variable.

***These errors that occur during runtime are called exceptions. Now, let’s see how you can handle these exceptions.

For error handling here we used following keywords:

try, catch, throw & finally.

1) Try…..Catch

The try…catch construct has two main blocks: try and catch

try: The statements will be execute.

catch: Statement will be executed if an exception is thrown in the try-block.

//Syntaxtry {  
// code...
} catch (err) {
// error handling
}

Example :

try{alert('Hello world');alert(students); 
// here handle an error, because here student is string, we didn't use quotation or we didn't declare it before use. Notice that, here our programs never stops for this error, program will be executed.
alert('is it working properly?') }catch{alert('contains errors')}

2) try…finally

The finally-block will always execute after the try-block and catch-block() have finished executing. It always executes, regardless of whether an exception was thrown or caught.

//Syntaxtry {
tryCode - Block of code to try
}
catch(err) {
catchCode - Block of code to handle errors
}
finally {
finallyCode - Block of code to be executed regardless of the try / catch result
}

Example

//examplevar count = 0;
function foo() {
try {
return count;
} finally {
count++;
}
}
console.log(foo());
console.log(count);
// expected output: 0
1

3) try…catch…finally

You can also use the try…catch…finally statement to handle exceptions. The finally block executes both when the code runs successfully or if an error occurs.

//Syntaxtry {
// try_statements
}
catch(error) {
// catch_statements
}
finally() {
// codes that gets executed anyway
}
//exampleconst numerator= 100, denominator = 'a';

try {
console.log(numerator/denominator);
console.log(a);
}catch(error) {
console.log('An error caught');
console.log('Error message: ' + error);
}finally {
console.log('Finally will execute every time');
}
// expected output:NaN
An error caught
Error message: ReferenceError: a is not defined
Finally will execute every time

Noted: You need to use catch or finally statement after try statement. Otherwise, the program will throw an error

// Uncaught SyntaxError: Missing catch or finally after try.

4) Comments

We should to keeps our coding clean & easy so that anyone can read our understand ours coding. For this we can use comments, So that everyone understands which line we are using for which work.

here we use // for single line comments & /*………*/ for multiple comments.

//example// this is single line comments /** this is multiple line comments-1
*this is multiple line comments-2
*this is multiple line comments-3
*this is multiple line comments-4
*/

5) Block Bindings

Traditionally, the way variable declarations work has been one tricky part of programming in JavaScript. In most C-based languages, variables (or bindings) are created at the spot where the declaration occurs. In JavaScript, however, this is not the case. Where your variables are actually created depends on how you declare them, and ECMAScript 6 offers options to make controlling scope easier. This chapter demonstrates why classic var declarations can be confusing, introduces block-level bindings in ECMAScript 6, and then offers some best practices for using them.

6) Var Declarations and Hoisting

Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting.

//examplefunction student(name) {
console.log("Student Name:" + name);
}
catName("Fayez");// expected output:Student Name: Fayez

Frontend Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store