Essential Concepts of Javascript
Data Types
Data types basically specify what kind of data can be stored and manipulated within a program.A value in JavaScript is always of a certain type. For example, a string or a number.
There are six basic data types in JavaScript which can be divided into three main categories: primitive (or primary), composite (or reference), and special data types.
Six Data Types that are primitives:
- undefined
- Boolean
- Number
- String
- BigInt
- Symbol
Structural Types:
- Object
- Function
- Null
Data TypesDescriptionExample:
1.String
represents textual data'hello'
, "hello world!"
etc
2.Number
an integer or a floating-point number3
, 3.234
, 3e-2
etc.
3.BigInt
an integer with arbitrary precision900719925124740999n
, 1n
etc.
4.Boolean
Any of two values: true or falsetrue
and false
5.undefined
a data type whose variable is not initializedlet a;
6.null
denotes a null
valuelet a = null;
7.Symbol
data type whose instances are unique and immutablelet value = Symbol('hello');
8.Object
key-value pairs of collection of datalet student = { };
Error Handling
JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#.
try
{
// code that may throw an error
}
catch(ex)
{
// code to be executed if an error occurs
}
finally{
// code to be executed regardless of an error occurs or not
}
- try: wrap suspicious code that may throw an error in try block.
- catch: write code to do something in catch block when an error occurs. The catch block can have parameters that will give you error information. Generally catch block is used to log an error or display specific messages to the user.
- finally: code in the finally block will always be executed regardless of the occurrence of an error. The finally block can be used to complete the remaining task or reset variables that might have changed before error occurred in try block.
Hoisting
In JavaScript, a variable can be declared after it has been used.In other words; a variable can be used before it has been declared.Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Basically, it gives us an advantage that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
Constants VS Let Declarations
The “let” and “const” are two different ways of creating variables. We all are familiar with the var keyword, which is used to create variables. Although, you can still use var to create variable but you are highly encouraged to use let and const instead.
The let is actually used where you need to reassign the value of the variable but when you don’t need to change the value of the variable you can use const. Reassigning the value to the const type variable will trigger an error.
Arrow Functions
An arrow function is one of the highly accepted and easy syntaxes of ES6. Before arrow functions were introduced, we had normal functions with the syntax.
let add = function(x,y) {
return x + y;
}
console.log(add(10,20)); // 30
The following example uses an arrow function which is equivalent to the above add()
function expression:
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;
Spread Operator
Let’s talking about the new syntax of ES6!! Yes, it is the spread operator. The Spread operator (…) is an operator provided by ES6 that allows us to easily obtain a list of parameters from an array. Actually, spread operators have the syntax of … (3 dots).
Default Parameter Value
Let’s move towards the new syntax of ES6! It is default parameter. Default parameters allows you to define a parameter in advance, which can be helpful in many scenarios. In JavaScript, our function parameters default to undefined. We now have the ability to set a different default value.
Comments
We generally used comments in our code to describe how and why the code works. By using comments we can explain our code and make it more readable. Comments are used to prevent execution when testing an alternative code. There are two types of comments we used: 1. Single line comments 2. Multi-line Comments.
Single line comments start with //. Any text between // and the end of the line will be ignored by JavaScript.
Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by JavaScript.
Coding Style
When we write code, sometimes we don’t try to make it clear, fresh. Most of the time we don’t use any kind of comment in our code as well. But it’s not a good practice. There are no fixed rules to maintain the way to beautify a code. But we can follow some steps and ideas that will help to make our code clear and more readable as well.
- Use === Instead of ==
- Utilize JS Lint.
- Declare Variables Outside the Loop Statement.
- Comment Your Code.
- Use [] Instead of New Array().
- Long list of variables to define.
- Use ES6 coding standard.
- Space after statement.
- Use semicolon is mandatory.
Conclusion
And there we go!So we can now implement this modern syntax, and bring our JavaScript to the next level!