JavaScript Essential Features
JavaScript is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it’s used in many non-browser environments as well. This language has some features that makes programmer ease. Here I am going to talk about some features of JavaScript.
Function: Function is a chunk of code designed for solving specific task. Function is only executing when it is called.
Function example:
function add(a, b){console.log(a + b);}add(2,5);
output: 7
Function with default parameter values: Default parameter allows us to pass a default values into the function but the default parameter only works when user doesn’t pass any parameter or parameter got undefined.
Example:
function say(message=’tested’) {console.log(message);}say();
Output: tested
Here, no parameter is passed that’s why default parameter works.
Arrow function: Arrow function allows us to define JavaScript function quickly by using arrow (=>) operator. We can use arrow function with parameter, without parameter even with multiple statement.
Example:
const add = (a, b) =>{console.log(a + b);}add(5, 5);Output: 10
Spread operator: Spread operator is mostly used in the variable array where there is more than 1 values are expected. Spread operator can be used in many cases like when we want to expand, copy, concat with math object.
Example:
const a = [2,5,6];const s=[0, 1, …a];console.log(s);
output: [ 0, 1, 2, 5, 6 ]
Block-level function: Block level function allows us to use function into a block of code and outsider of the block can’t execute that function.
Example:
{function block(){console.log(“block-function”);}}
Declaration and Hoisting: Hoisting is JavaScript’s default behavior of moving declarations to the top. In ES6 a variable can be declare again after it has been used.
Example:
var x; // Declare x
x = 5; // Assign 5 to x
elem = document.getElementById(“demo”); // Find an element
elem.innerHTML = x;
block binding in loop: We know that when we declare a variable with var, it will be hoisted to the top of the function. So even if we declare the variable within a for loop, we can access it from outside of the loop as well. In other languages where Block level is default scope, the i variable will be accessible only in the for loop and will not be accessible from outside. So to fix this in JavaScript, ES6 has introduced the let & const datatypes.
Example:
for (let i = 0; i < 5; i++){console.log(i);}
In that case let i only accessible inside the loop.
Error handling: No matter how great programmer we are. Sometimes our scripts got some error. They may be occurring because of our mistake, invalid server response or invalid user input. If we are not handling that error, it might create an effect on our expected output. There is syntax called (try and catch) by which we can solved that problem.
Syntax of try catch:
try{//code}catch(err){//code for handing}
How it’s working: At first it’s executing the try statement if not found any error the execution won’t go farther catch statement and if any error occurs in try statement then the catch statement will execute.
Error object: when an error occurs JavaScript create an object that tell us details about error.
We can pass that object as an argument to the catch and print that object. All build in errors has two main properties. They are name for an undefined variable that’s “ReffrenceError” and another one is message that describe about the error.
Example:
try {rashed;} catch (err) {console.log(err.name);console.log(err.message);}
Output:
ReferenceError
rashed is not defined
Handling own error: We can also handle our own user defined error. For understand this let’s check out the example.
Example:
let json = ‘{ “age”: 30 }’;try {let user = JSON.parse(json);if (!user.name) {throw new SyntaxError(“Incomplete data: no name”);}console.log( user.name );} catch (err) {console.log( “doesn’t execute” );}
Here Json.parse works fine, the actual error is that the missing of user.name. when condition check that user.name is not available it will throw a syntax error and stop to executing the try part then catch pick that error and continuing to executing his part.
Try catch and finally: Try is trying to find the error if no error occurs it will works fine and not executing the catch part. If errors occur, then it will stop executing to try part and going for executing of catch part. The main twist is that the final part is always executing whether the errors occurs or not.
Basic structure:
try {… try to execute the code …} catch (err) {… handle errors …} finally {… execute always …}