0% found this document useful (0 votes)
15 views

Javascript 4

Uploaded by

pbt8112001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Javascript 4

Uploaded by

pbt8112001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

JAVASCRIPT

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

Common JS ES6 Modules


CommonJS
CommonJS is a set of standards used to implement modules on JavaScript.
CommonJS is mainly used in server-side JS apps with Node, as browsers don't support the use of
CommonJS.
Suppose you create Another file mod1.js now in this file you have created a function

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.

Cross-Platform Compatibility: Developers can target a wider range of environments, including


older browsers and devices, ensuring seamless compatibility without compromising code
quality.

Performance Optimization: Transpilers optimize JavaScript applications by converting code into


earlier versions, reducing download and execution requirements, thus improving page load
times and overall application performance.
Babel
Well, all the latest javascript features are not supported in every browser yet.
So Something is required to convert it.
babel is here to transpile latest javascript features(ES6 features) which are not understandable to
every browser to ES5 which is understandable to every browser.
Declarative/ Imperative
Declarative programming is a paradigm describing WHAT the program does, without explicitly
specifying its control flow.

Imperative programming is a paradigm describing HOW the program should do something by


explicitly specifying each instruction (or statement) step by step, which mutate the program's
state.

You might also like