Let & Const
let
and const
basically replace var
.
You use let
instead of var
, and const
instead of var
if you never plan to reassign this "Variable" (Meaning effectively turning it into a constant)
To Read more about this let
Please visit here
Also checkout about const
here
Arrow Functions
Arrow functions are a different way of creating functions in JavaScript.
It has a very simple syntax.
This normal JS Function
function callMe(name) {
console.log(name);
}
becomes:
const callMe = (name) => {
console.log(name)
}
Important When Having No Arguments, we have to use empty parentheses in the function declaration
Example :
const callMe = () => {
console.log('Sumit')
}
If we have just arguments being passed to the function, we can write the functions as
const callMe = name => {
return name;
}
So basically there is no need of parentheses, this only applies if there is exactly one arguments being passed. If there are more than one arguments or no arguments .. in that case the above will not make sense
For Example , this will not work
const callMe = name,age => {
return name;
}
If we want to return just a single line of code, then we can use the below syntax
const callMe = name => name
which is same as
const callMe = (name) => {
return name;
}
Please refer here for more information
In all modern Javascript projects, we ususally split our codes in multiple files so that we can manage them easily.
To still access functionality in another file , we need to export
(to make it available) and import
(to get access).
I will walk you through this with examples, so that it will be clear.
we have two types of exports, named
and unnamed (default)
export default ...;
export const someName = ...;
We can import default
exports like
import somenameofyourchoice '../path/to/jsfile';
Note: Here somenameofyourchoice is totally up to you, you can use any name here.
Named Exports have to be imported by their name
import {someName} from '../path/to/jsfile';
One thing here we need to keep in mind is that, in one file we can have only 1 default export and an unlimited number of named exports.
while using the keyword export, if we dont mention default keyword, it will automatically condider it as a named export
To read more about exports, check here
To read more about imports, check here