More Fundamentals About JavaScript That You Should Know

When you talk about any programming language a very common word that we all should know is error. No matter how great you are at programming, sometimes our code has errors. Most of the time it occurs because of our mistakes. So, in this blog, the first topic that we are going to introduce is called Error Handling.

There's a common syntax try-catch that helps us to handle errors so that we can do more reasonable code.

1. try....catch

It has two main blocks try and then catch. The basic structure of try-catch syntax is

                      try {
                            // code
                             }

                       catch(error) {

                            // handling error

                             }

To handle an error the try-catch syntax always works in a flow like this

  1. The code is executed in try { }
  2. If there were no error catch(error) is ignored and the next part of the execution reaches the end of try{..}
  3. If any error found then try execution is stopped and then it flows into catch(error)

So basically, if an error occurs into try{...} the entire execution doesn't get stopped and we have catch(error) to handle it.

2.Coding Style

No matter how great you are at programming you should follow a coding Style that makes your code easy to read and also efficient. So, here is some example that we should follow as given below

3. Curly Braces Most of the time we write the opening bracket on the same line as a similar keyword instead of a new line. There should also be a space before the opening bracket.

Right Way

                             if (condition) {
                                       //do something
                                 }

4. Line Length It's really awkward to read a long horizontal line of code. So, we should split them for the best practice

    let title = ` always try to follow the best way of coding 
                      style it will make your code easy to read `;

5. Semicolons We should always use semicolons after each statement. Though the use of semicolons after a statement is optional in some language. The majority of developers put semicolons.

               const name = "John";
                console.log("My name is " +name);

6.Comments

An important sign of being a good developer is using comments on your code. It helps in maintaining our code. We normally use comments to describe how and why the code works. Comments can be a single line and multiple lines. In a good code, the number of comments should be minimal.

               // This is a single line comment 
               /* Multiple line comments should 
                write in this way*/

In JavaScript, there is one more thing that we should know is JavaScript data types. So, now we are going to describe some of the JavaScript data types that given bellow

7. Primitive Values

In javaScript, a primitive value is a data that is not an object and has no methods. These are the values that can't be changed. Basically, primitive values are immutable.

                        console.log( "Hello" );
                        console.log( 2 );

8. Object and Functions

Object and functions are also values but they are not primitive. In JavaScript object can be seen as a collection of properties. An object is a mapping between keys and values. Keys are strings (or Symbols), and values can be anything.

On the other hand, JavaScript doesn't have a function data type. Functions are regular objects with the additional capability of being callable.

Structure of an object:

          const person = {
                        name: "John" ,
                        age: "25"
                           } ;

Structure of a function:

               funtion add (a, b) {
                                   var sum = a + b ;
                                    return sum;
                                   }