ECMAScript Modules:
JavaScript standard from 2015 introduces its own, different module system. It is usually called ES modules, where ES stands for ECMAScript. ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.
ES Modules Syntax: The syntax to export and import. An HTML page can add a module by using a <script> tag with the special type=”module” or nomodule attribute:
Export:
The export statement is used when creating JavaScript modules to export functions, objects, class or primitive values from the module so they can be used by other programs with the import statement. Many way use export. Example:
Exporting individual features: Every functions, objects, class before use export.
Example:
Export Default : There are two different types of export, named and default. We can have multiple named exports per module but only one default export per file. Each type corresponds to one of the above syntax:
Example:
Export list: we can put export separately. Here we first declare, and then export.
Example:
Renaming Exports: We can also use as to export under different names(change name).
Example:
Import:
Import – The static import statement is used to import bindings which are exported by another module. Imported modules are in strict mode whether we declare them as such or not. Many way use import. Example:
Importing Named: We can have multiple named exports per module. Named exports are useful to export several values. During the import, it is mandatory to use the same name of the corresponding object.
Example:
Importing Default: We can have only one default export per module. A default export can be imported with any name.
Example:
Importing Default and Named: We can have only one default export per module. A default export can be imported with any name.
Example:
Importing All: Import all of a module’s exports as a module object.
Example:
Asynchronous Programming
What is Asynchronous?
In general, asynchronous comes from Greek asyn-, meaning “not with,” and chronos, meaning “time” is an adjective describing objects or events that are not coordinated in time.
In computer programs, asynchronous operation means that a process operates independently of other processes, whereas synchronous operation means that the process runs only as a result of some other process being completed or handing off operation.
In a synchronous way the first operation is start first and complete its execution then the next operation is start after complete the first one. Next operation is blocked by the previous operation until complete it. But in asynchronous programming one task never wait for another task for execute. The task execute first which completed first. It never block one task input or output for another task.
JavaScript is an asynchronous programming language and java is a synchronous language.
Example 1: Synchronous programming java
In this example after execute first line it will wait for 5 sec, then second and third line will be executed. Also wait 4 second after third line and execute fourth and fifth line.
Example 2: Asynchronous programming JavaScript.
In this example using JavaScript no line wait for another line. Here second line does not wait for first line and forth line does not wait after third line. These all lines are execute at run time. No line blocked another line. That is non-blocking of asynchronous programming.
There are different way to implement Asynchronous in JavaScript.
- Callback function
- Promise
- Generators
- Async & Await
I will describe here about Callback function and Promise.
Callback function:
Callback function is a function that return or use another function as it’s parameter.
Callback is an easier way to describe asynchronous programming.
Example:
In this example setTimeout function uses another function to display second line. And this timeout function set 5 sec waiting time for second line and 4 sec for fourth line.
But in execution third and fifth line does not wait for second and fourth line. They are executed after first line. Then fourth line is executed before second line because second line needs more time.
Here second line need 5 sec . But it can’t block the execution of another line after it. So this is an non-blocking process and this process is defined by asynchronous.
Promise:
Promise is a build in object in JavaScript(ES6).
Promises are the way to deal with asynchronous programming without writing too many callback in function.
It is begin difficult to maintain the program when the chain of callback functions is so long to implement lots of asynchronous task. To solve this problem we can use promise. It is a clean way to implement asynchronous programming in JavaScript.
Promises give us a way to handle asynchronous processing in a synchronous way. They represent a value that we can handle at some point in the future. And, better than callbacks here, Promises give us guarantees about that future value, specifically:
- The promise is immutable: No other registered handlers of that value can change it.
- We are guaranteed to receive the value, regardless of when we register a handler for it, even if it’s already resolved.
For example:
Creating promise:
To create an object we can use class in ES6. We can also use a constructor with it. Constructor is used to give property values in object creation time. To deal with promise there are two methods. Then() and catch(). That works as chain
In this way a promise has three steps.
1.Pending: until a Promise is fulfilled it is in pending state
- Fulfillment: when the first handler is called the Promise is considered fulfilled with the value passed to that handler.
- Rejected: if the second handler is called, the Promise is considered rejected with the value passed to that handler.
The standard way to create a promise is to use new Promise construction with accept a handler that is given two function as parameter. The first handler is resolve and another is reject.
Example:
The then() method takes a function that will be passed the resolved value of the Promise when it is fulfilled. And the catch() method takes a single handler to be called when a promise is rejected.
Md. Hamidur Rahaman
Expressions and statements are the bread and butter of a program’s code structure. In this blog I try to explain the concept of expressions and statements.
Expressions are fragments of code that produces a new value as a result. For example it is similar to arithmetical operation. By arithmetical operations we can do addition or subtraction between two numbers or more to create a new number as the result. There is an easy way to spot expressions is to look for the operator sign in the code.
There are different types of expressions:
- Arithmetic Expressions :
- String Expressions:
- Array and Object Expressions:
- Logical Expressions :
- Object Creation Expressions:These expressions return an object
- Object Property Expressions:
- Function Definition Expressions:
On the other hand, Most JavaScript programs contain JavaScript statements . The statements are executed, one by one in the same order as they are written.
We can see that , the statements are executed one by one and after statement 4 the result is executed.
Reference: https://eloquentjavascript.net/
- 1
- 2