Some random topic of javascript
Today I’ll briefly discuss some topics of javascript. Which I get after studying previous some days.
- Coding Style
Coding style is very important for programmers. Because if we don’t follow a standard style it will be very difficult to understand one’s code to another. During coding, we should alert about some things like syntax, curly brace, line length, indention, semicolon, nesting level, function placement, function name, etc.
But the question is which style we should follow? Don’t worry, there are some popular styles which most developers follow while they write their code. Some popular styles are… Google JavaScript Style Guide, Airbnb JavaScript Style Guide, IdiomaticJS, StandardJS, etc.
2. Comments
Comments are available in every programming language. Using comments in code is really helpful, It helps to remember what exactly done before or what to do now. But there is some good and bad practice in commenting. Too much commenting on the code is annoying and unuseful. On the other hand, a short and brief comment which describes the code helps to understand remember the architecture of our code.
3. Error Handling
There is no programmer who doesn’t get errors in their code. So, when we get errors we need to handle them. JavaScript has a syntax for handling errors which is “Try-Catch”. If there is a possibility of a code to crush during executing then we put them in the try block. If an error happened, the catch block executes and provides the error message. There is also has a finally block that executes after try-catch execution.
4. Blocks
Block means boundary. That means the block does specify the executing area of a code. curly braces of function or loop is an example of the block.
5. Execution Context
Execution context is related to block. the execution context of a function is the execution area inside of the function. When a function is called, the execution context will be created. Codes inside of the function will be executed then.
6. Var vs Let vs Const
In javascript, we can declare variables in 3 ways. the are var, let, and const. If we declare a variable using var that will be attached to the global object. so we can access that from anywhere. We can also reassign the value of this variable. Let is a block-level variable. if we use let to declaring the variable, we can only access and modify them from its own and its child executing context. Const is similar to let. but the difference is we cannot reassign its value.
7. Functions
Functions are used to prevent repeating code. It is one of the building blocks of any programming language. We create a function for specific work and repeat this work by calling this function.
fucntion add(x, y){
console.log(x+y)
}
add(3,4) // 7
add(5,6) // 11
8. Default Parameters
As we know we can pass parameters in the function. But, sometimes we don’t pass the value of these parameters. In this situation, we get an undefined value. To preventing this situation we can provide the default value of the parameters. It's called the default parameter.
function multiply(x, y=1){
console.log(x*y)
}
9. Spread Operator
The spread operator is the latest addition to ES6. It is very useful. It reduces the code. We can create a copy of an array without modifying the original array using the spread operator.
const colors = ['black', 'blue', 'green']
const newColors = [...colors] // newColore is the new array
10. Arrow Function
Arrow function is also the new addition of ES6. Its syntax is simple and easier. The main difference between the arrow function and normal function is… If we use ‘this’ keyword in the arrow function, it will refer to the parent object where the normal function refers current object. Also, we cannot declare the arrow function using function declaration.