Javascript 4
Javascript 4
Functions
•A function is a block of code that performs a specific task.
• JavaScript functions are basically used to encapsulate logic, making
that code more reusable and easier to understand.
•In JavaScript, functions are first-class objects, because they can be
passed to other functions, returned from functions, and assigned to
variables and properties.
Function Statement
Function as Variable (function
Expression)
Arrow Function
•The arrow function syntax is a shorthand version of the regular function syntax
•ES6 Arrow functions enable us to write functions with simpler and shorter
syntax and make our code more readable and organized.
•The arrow functions are introduced in the ES6 version.
•Arrow functions provides us with a more precise approach to writing JavaScript
Functions.
Arrow function
Arrow function
Array
In JavaScript, an array is an object constituted by a group of items having a specific order.
Arrays can hold values of mixed data ty
pes and their size is not fixed.
Length
PUSH
POP
Shift
Unshift
IndexOf
Includes
Join
Slice
Splice
Splice
Objects
JavaScript is designed on a simple object-based paradigm.
An object is a collection of properties, and a property is an association between a name (or key)
and a value.
Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a
design, weight, a material it is made of, etc. The same way, JavaScript objects can have
properties, which define their characteristics.
Object
• In JavaScript, almost "everything" is an object.
• Booleans can be objects (if defined with the new keyword)
• Numbers can be objects (if defined with the new keyword)
• Strings can be objects (if defined with the new keyword)
• Dates are always objects
• Maths are always objects
• Regular expressions are always objects
• Arrays are always objects
• Functions are always objects
• Objects are always objects
Object Literal Method
Class
In JavaScript, Classes define blueprints for creating objects with similar properties and methods.
Classes are similar to functions. Here, a class keyword is used instead of a function keyword.
Unlike functions classes in JavaScript are not hoisted. The constructor method is used to
initialize. The class name is user-defined..
Modules
A module in JavaScript is just a file containing related code.
A module is just a piece of code in a file that you can call and use from other files.
In JavaScript, we use the import and export keywords to share and receive functionalities
respectively across different modules.
The export keyword is used to make a variable, function, class or object accessible to other
modules. In other words, it becomes a public code.
The import keyword is used to bring in public code from another module.
Two Ways of creating Module
Module
module.exports is the keyword we use to declare all we want to export from that file.
To use this function in our main.js file, we can do it like this:
If We want to export more than one thing from a single module then we can do it like this:
ESmodules
ESmodules is a standard that was introduced with ES6 (2015).
ESmodules is a more modern approach that is currently supported by browser and server-side
apps with Node.
With ES Modules we use Import Keyword to use method in different file
Another feature available in ESmodules is import renaming,
which can be done like this:
Transpiler
A transpiler, short for "transformation compiler," is a tool that converts code written in one
programming language into another programming language, while preserving the original
functionality.
In the context of JavaScript, transpilers are used to convert code written in the latest version of
JavaScript (ES6 or later) into an earlier version that is more widely supported by browsers and
other environments.
Why are Transpilers Useful?
Modern Syntax Adoption: Transpilers empower developers to utilize the latest JavaScript syntax,
enhancing code readability and maintainability.