10 Important topics in JavaScript
Let’s start to know about some important topics about JavaScript which are very significant to know as a JavaScript Developer.
1. Error handling, “try…catch”:
Every programmer get error. No matter who is great in programming or not. We face error in our daily life. so, we should know about try…catch for our error handle.
“try…catch” syntax :
try{
// some code
}catch{
// error handling
}
In this above structure, there have two word try and catch. Let’s discuses how it’s work:
1. At first, code is executed in try{//some code}. Write some code insert of try structure.
2. If there have no error, catch is ignored.
3. If found any error it's going to catch{//error handling} structure.
Some Example:
If code have not find any error, it’s not going to catch(err) statement:
If code have face error, it’s going to catch(err) statement:
Look at this code, at first it’s console the “this is test1” line. then it’s going to three number line and find a error, so this error going to catch(err) statement and stop this code in here.
so, the output is =
Error object:
There are three main properties for error:
name:
This is error name. Above we see in code line 7, the error message is “ReferenceError ”due to undefined variable from line number 3.
message:
This is message about error details. Above we see in code line 8, the error message is “result is not defined” due to undefined variable from line number 3.
stack:
Current call stack
so let’s see the output:
Example of error:
// RangeError
try{
var value = 1;
value.toPrecision(700) // RangeError
}catch(err){
console.log(err.name)
}// ReferenceError
try{
value = y + 1;// ReferenceError
}catch(err){
console.log(err.name)
}// TypeError
try{
let value = 10
Number.toUpperCase(); // TypeError
}catch(err){
console.log(err.name)
}// URIError:
try{
decodeURI("%%%") // URIError
}catch(err){
console.log(err.name)
}
We know that, JavaScript supports the JSON.parse(str) method. it’s like this:
If we write something wrong with the data and insert it try…catch statement:
2. Coding style
We need to write code using follow some style. it should be clean and easy to read. Good code style is actually the art of programming. All programmer must write coding using maintain the good code style.
Let’s start to know some suggested rules about coding style:
- Space between parameter and curly braces:
Here, line number 1, need to space between parameter and for curly braces need to write on the same line after a white space.
2. Line Length:
If you need to write multiple lines, best practice to split them.
3. Indentation 2 or 4 spaces:
We need to maintain the indentation for writing clear and readable code
4. A space after if/for/while and empty line:
We should maintain about space after for/if/while loop and also need an empty line between logical blocks.
Example of good coding style😃:
Example of bad coding style😠:
3. Comments:
When any developer write code with comment, then it will be easy for understanding any developer when they read this. It’s help to understand, how the code it’s working, purpose of working, and also easy to find any structure
There are two types of comments:
- single line comment which is “//”
- multiple line comment which is “/*…*/”
We normally use comments for describe how the code works and why this code.
4. let keyword:
let keyword allows a variable with block scope.
Example:
Here, firstly, number was declare 10, then declare this number using let in block scope. this number variable only we can use in block scope. it’s not working outside of block scope.
that’s why, the result should be like this:
5. const keyword:
This const keyword similar to let keyword except that the value cannot be changeable. This const keyword use for to declare a constant value.
Example:
If we write like this:
We knew, the value cannot be changed in const keyword. But, here for trying to change the number was 11 to 12. that’s why it give an error and this error is:
6. Classes:
For creating JavaScript class, just use the “class ”keyword and add a method name “constructor()”. class is very useful for insert multiple variable. A JavaScript class is not a object, it is a template for JavaScript objects.
Above the example creates a class which name is “parson”. And there have two properties which are “name” and “age”.
syntax:
class ClassName {
constructor() {
...
}
}
Example:
Output above code:
7. Default Parameter values:
ES6 allows default values in function parameters.
Above the function, b is 2 if not passed or undefined.
8. Function Rest Parameter:
(…), this is call rest parameter. the rest parameter allows us to treat an indefinite number of arguments as an array.
9. Array.find():
The find() method as like a condition. Using find() method , we can find the matching value of the first array element.
10. Array.findIndex():
The findIndex() method also like a condition. Using findIndex() method , we can find the matching value of index number of the first array element. The main difference between findIndex() and find() are, findIndex() return the index of matching number and find() return the value of matching number.