Some JavaScript fundamentals
Function
Functions are the most fundamental building block of every programming language. A function in JavaScript is similar to the other programming language. A function is a procedure or a set of statements that performs a task or calculates a value. Generally function takes arguments and performs some task and return a value.
In this article we are taking about some types of function with example.
Default function parameters
When we pass a parameter in a function and set a default value of this parameter, we don’t pass any value or pass undefine then it’s called default function.
Spread operator
Spread syntax (...
) allows an iterable such as an array expression or string. For array it can be inerrable and for object spread operator expanded in places where zero or more key-value pairs.
Arrow function
Arrow function in JavaScript is a compact alternative declaration of a function. It doesn’t follow the tradition syntax for a function. Generally it’s a way to declare a function in a short form.
try-catch
Sometime programmers makes mistake even they are good at programming. And for the reason an error occurs. It may be scripts have errors, unexpected user input, an erroneous server response, and thousand other reasons. Then script “dies” (immediately stops) in case of an error and printing it to console. But this is not a good practice for a programmer. But there’s a syntax construct try catch that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.
Block binding
Variable declarations is one of the tricky part in of programming in JavaScript. Where in most C-based language variable are created at the spot where declaration occur. But in JavaScript variable are created depends on how you declare them and from where you use them.
Var Declarations and Hoisting
In JavaScript you can declare a variable using var keyword. Var is a functional scope variable that means when you declare a variable using var it can be access from anywhere inside the function. This is called hoisting.
Block-Level Declarations
When you declare a variable it’s declaration and access level can be two type. Inside of a function
- Inside of a function.
- Inside a block { }
Declare variable using var keyword is function level declaration of variable.
Declare variable using let or const keyword is block level declaration of variable.
Block Binding in Loops
When we declare a variable in loop using var then it can be access from the outside of the loop. It may conflict and that can be give a unexpected output. For this reason block binding comes. We can bind a variable within loops using block binding. To bind a variable withing loop we have to declare a variable using let.
Global Block Binding.
When we declare a variable globally or a window variable then it called global binding. Global variable can be access anywhere of the program.
Comment in JavaScript
Comment is very important issue when we write a code. It will happen we come back to write code after long time. Then we can’t understand our code what the purpose of declare variable and what the function do. To avoid that kind of worst situation we need to comment for better understanding, There is a good option to avoid unusual comment. We can declare our variable and function is a meaningful mane.