Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript

JavaScript is a lightweight, cross-platform programming language primarily used for web development, capable of both client-side and server-side applications. It is weakly typed, supports imperative and declarative programming, and has a rich set of libraries and frameworks. JavaScript can be linked to HTML either internally or externally, and it has various applications including web development, server applications, and game development, though it also has limitations such as security risks and performance issues.

Uploaded by

arunbaditya1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript

JavaScript is a lightweight, cross-platform programming language primarily used for web development, capable of both client-side and server-side applications. It is weakly typed, supports imperative and declarative programming, and has a rich set of libraries and frameworks. JavaScript can be linked to HTML either internally or externally, and it has various applications including web development, server applications, and game development, though it also has limitations such as security risks and performance issues.

Uploaded by

arunbaditya1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-3

JavaScript is a lightweight, cross-platform, single-threaded, and interpreted


compiled programming language. It is also known as the scripting language
for webpages. It is well-known for the development of web pages, and many
non-browser environments also use it.
JavaScript is a weakly typed language (dynamically typed). JavaScript
can be used for Client-side developments as well as Server-
side developments. JavaScript is both an imperative and declarative type of
language. JavaScript contains a standard library of objects, like Array, Date,
and Math, and a core set of language elements like operators, control
structures, and statements.
 Client-side: It supplies objects to control a browser and its Document
Object Model (DOM). Like if client-side extensions allow an application to
place elements on an HTML form and respond to user events such
as mouse clicks, form input, and page navigation. Useful libraries for
the client side are AngularJS, ReactJS, VueJS, and so many others.
 Server-side: The server side work involves things like communicating
with database, manipulating files and generating response. With runtime
environment Node.js that allows JavaScript to run outside a browser and
Frameworks like Express.JS, JavaScript is now being widely used on the
Server side.
 Imperative language – In this type of language we are mostly concerned
about how it is to be done. It simply controls the flow of computation. The
procedural programming approach, object, oriented approach comes
under this as async await we are thinking about what is to be done further
after the async call.
 Declarative programming – In this type of language we are concerned
about how it is to be done, basically here logical computation requires.
Her main goal is to describe the desired result without direct dictation on
how to get it as the arrow function does.
For a more structured approach to learning JavaScript, our JavaScript
Course offers a complete guide, from the basics to advanced features.
Whether you’re just starting or looking to refine your skills, this course will
help you become proficient in JavaScript.
How to Link JavaScript File in HTML ?
JavaScript can be added to HTML file in two ways:
 Internal JS: We can add JavaScript directly to our HTML file by writing
the code inside the <script> tag. The <script> tag can either be placed
inside the <head> or the <body> tag according to the requirement.
 External JS: We can write JavaScript code in another files having an
extension.js and then link this file inside the <head> tag of the HTML file
in which we want to add this code.
Syntax:
<script>
// JavaScript Code
</script>

1
<!DOCTYPE html>
2
<html lang="en">
3

4
<head>
5
<title>
6
Basic Example to Describe JavaScript
7
</title>
8
</head>
9

10
<body>
11

12
<!-- JavaScript code can be embedded inside
13
head section or body section -->
14
<script>
15
console.log("Welcome to GeeksforGeeks");
16
</script>
17
</body>
18

19
</html>
20

History of JavaScript
It was created by Brendan Eich in 1995. He was an engineer at Netscape. It
was originally going to be named LiveScript but was renamed. Unlike most
programming languages, JavaScript language has no concept of input or
output. It is designed to run as a scripting language in a host environment,
and it is up to the host environment to provide mechanisms for
communicating with the outside world. The most common host environment
is the browser. Please refer History of JavaScript for details.
Features of JavaScript
According to a recent survey conducted by Stack Overflow, JavaScript is
the most popular language on earth. With advances in browser technology
and JavaScript having moved into the server with Node.js and other
frameworks, JavaScript is capable of so much more. Here are a few things
that we can do with JavaScript:
 JavaScript was created in the first place for DOM manipulation. Earlier
websites were mostly static, after JS was created dynamic Web sites
were made.
 Functions in JS are objects. They may have properties and methods just
like other objects. They can be passed as arguments in other functions.
 Can handle date and time.
 Performs Form Validation although the forms are created using HTML.
 No compiler is needed.
Applications of JavaScript
 Web Development: Adding interactivity and behavior to static sites
JavaScript was invented to do this in 1995. By using AngularJS that can
be achieved so easily.
 Web Applications: With technology, browsers have improved to the
extent that a language was required to create robust web applications.
When we explore a map in Google Maps then we only need to click and
drag the mouse. All detailed view is just a click away, and this is possible
only because of JavaScript. It uses Application Programming
Interfaces(APIs) that provide extra power to the code. The Electron and
React are helpful in this department.
 Server Applications: With the help of Node.js, JavaScript made its way
from client to server and Node.js is the most powerful on the server side.
 Games: Not only in websites, but JavaScript also helps in creating games
for leisure. The combination of JavaScript and HTML 5 makes JavaScript
popular in game development as well. It provides the EaseJS library
which provides solutions for working with rich graphics.
 Smartwatches: JavaScript is being used in all possible devices and
applications. It provides a library PebbleJS which is used in smartwatch
applications. This framework works for applications that require the
Internet for their functioning.
 Art: Artists and designers can create whatever they want using
JavaScript to draw on HTML 5 canvas, and make the sound more
effective also can be used p5.js library.
 Machine Learning: This JavaScript ml5.js library can be used in web
development by using machine learning.
 Mobile Applications: JavaScript can also be used to build an application
for non-web contexts. The features and uses of JavaScript make it a
powerful tool for creating mobile applications. This is a Framework for
building web and mobile apps using JavaScript. Using React Native, we
can build mobile applications for different operating systems. We do not
require to write code for different systems. Write once use it anywhere!
Limitations of JavaScript
 Security risks: JavaScript can be used to fetch data using AJAX or by
manipulating tags that load data such as <img>, <object>, <script>. These
attacks are called cross-site script attacks. They inject JS that is not part
of the site into the visitor’s browser thus fetching the details.
 Performance: JavaScript does not provide the same level of performance
as offered by many traditional languages as a complex program written in
JavaScript would be comparatively slow. But as JavaScript is used to
perform simple tasks in a browser, so performance is not considered a big
restriction in its use.
 Complexity: To master a scripting language, programmers must have a
thorough knowledge of all the programming concepts, core language
objects, and client and server-side objects otherwise it would be difficult
for them to write advanced scripts using JavaScript.
 Weak error handling and type checking facilities: It is a weakly typed
language as there is no need to specify the data type of the variable. So
wrong type checking is not performed by compile.
Why JavaScript is known as a lightweight
programming language ?
JavaScript is considered lightweight due to the fact that it has low CPU
usage, is easy to implement, and has a minimalist syntax. Minimalist syntax
as in, has no data types. Everything is treated here as an object. It is very
easy to learn because of its syntax similar to C++ and Java.
A lightweight language does not consume much of your CPU’s resources. It
doesn’t put excess strain on your CPU or RAM. JavaScript runs in the
browser even though it has complex paradigms and logic which means it
uses fewer resources than other languages. For example, NodeJs, a
variation of JavaScript not only performs faster computations but also uses
fewer resources than its counterparts such as Dart or Java.
Additionally, when compared with other programming languages, it has
fewer in-built libraries or frameworks, contributing as another reason for it
being lightweight. However, this brings a drawback in that we need to
incorporate external libraries and frameworks.
Is JavaScript Compiled or Interpreted or both ?
JavaScript is both compiled and interpreted. In the earlier versions of
JavaScript, it used only the interpreter that executed code line by line and
shows the result immediately. But with time the performance became an
issue as interpretation is quite slow. Therefore, in the newer versions of JS,
probably after the V8, the JIT compiler was also incorporated to optimize the
execution and display the result more quickly. This JIT compiler generates a
bytecode that is relatively easier to code. This bytecode is a set of highly
optimized instructions.
The V8 engine initially uses an interpreter, to interpret the code. On further
executions, the V8 engine finds patterns such as frequently executed
functions, and frequently used variables, and compiles them to improve
performance.
JavaScript is best known for web page development but it is also used in a
variety of non-browser environments.
Functions in JavaScript
Last Updated : 09 Oct, 2024



A function in JavaScript is a reusable block of code that performs a


specific task. You define it once, and then you can run (or “call”) it
whenever you need that task done in your program.
A JavaScript function runs when it is “called” by some part of your
code.
Syntax: The basic syntax to create a function in JavaScript is shown
below.
function functionName(Parameter1, Parameter2, ...)
{
// Function body
}
To create a function in JavaScript, we have to first use
the keyword function, separated by the name of the function and
parameters within parenthesis. The part of the function inside the
curly braces {} is the body of the function.
In javascript, functions can be used in the same way as variables for
assignments, or calculations.
To dive deeper into functions and learn advanced techniques like
closures and recursion, check out our JavaScript Course, which
covers functions in detail with hands-on examples and exercises.
Why Functions?
 Functions can be used multiple times, reducing redundancy.
 Break down complex problems into manageable pieces.
 Manage complexity by hiding implementation details.
 Can call themselves to solve problems recursively.
Function Invocation
The function code you have written will be executed whenever it is
called.
 Triggered by an event (e.g., a button click by a user).
 When explicitly called from JavaScript code.
 Automatically executed, such as in self-invoking functions.
Function Definition
Before, using a user-defined function in JavaScript we have to create
one. We can use the above syntax to create a function in JavaScript.
A function definition is sometimes also termed a function declaration
or function statement. Below are the rules for creating a function in
JavaScript:
 Every function should begin with the keyword function followed
by,
 A user-defined function name that should be unique,
 A list of parameters enclosed within parentheses and separated
by commas,
 A list of statements composing the body of the function enclosed
within curly braces {}.
Example: This example shows a basic declaration of a function in
javascript.
JavaScript

function calcAddition(number1, number2) {


2

return number1 + number2;


3

}
4

console.log(calcAddition(6,9));
5

Output
15
In the above example, we have created a function
named calcAddition,
 This function accepts two numbers as parameters and returns the
addition of these two numbers.
 Accessing the function with just the function name without () will
return the function object instead of the function result.
There are three ways of writing a function in JavaScript:
Function Declaration: It declares a function with a function
keyword. The function declaration must have a function name.
Syntax:
function geeksforGeeks(paramA, paramB) {
// Set of statements
}
Function Expression
It is similar to a function declaration without the function
name. Function expressions can be stored in a variable assignment.
Syntax:
let geeksforGeeks= function(paramA, paramB) {
// Set of statements
}
Example: This example explains the usage of the Function
expression.
JavaScript

const square = function (number) {


2

return number * number;


3

};
4

const x = square(4); // x gets the value 16


5

console.log(x);
6

Output
16

Functions as Variable Values


Functions can be used the same way as you use variables.
Example:
// Function to convert Fahrenheit to Celsius
function toCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
// Using the function to convert temperature
let temperatureInFahrenheit = 77;
let temperatureInCelsius = toCelsius(temperatureInFahrenheit);
let text = "The temperature is " + temperatureInCelsius + "

You might also like