[Go to site: main page, start]

0% found this document useful (0 votes)
2 views13 pages

SEC Unit-I (JavaScript)

JavaScript, created by Brendan Eich in 1995, evolved from ES1 to ES6 and beyond, introducing modern features and expanding its use beyond browsers with Node.js. It includes various operators for mathematical and logical computations, control statements for program execution, and is distinct from VBScript and Java in terms of syntax and application. Additionally, JavaScript arrays and functions are essential for organizing data and performing tasks, while the Date object is crucial for handling date and time in web applications.

Uploaded by

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

SEC Unit-I (JavaScript)

JavaScript, created by Brendan Eich in 1995, evolved from ES1 to ES6 and beyond, introducing modern features and expanding its use beyond browsers with Node.js. It includes various operators for mathematical and logical computations, control statements for program execution, and is distinct from VBScript and Java in terms of syntax and application. Additionally, JavaScript arrays and functions are essential for organizing data and performing tasks, while the Date object is crucial for handling date and time in web applications.

Uploaded by

sakshi.kakade518
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Evolution of javascript

JavaScript was invented by Brendan Eich in 1995. It was developed for


the Netscape browser, and became the ECMA-262 standard in 1997.

After Netscape handed JavaScript over to ECMA, the Mozilla foundation


continued to develop JavaScript for the Firefox browser. Mozilla's latest
version was 1.8.5. (Identical to ES5).

Internet Explorer 4 was the first browser to support ECMAScript 1


(ES1).Internet Explorer 10 was the first browser to support ECMAScript
5 (ES5). Internet Explorer 11 was the last version of Internet Explorer.
Internet Explorer 11 primarily supported ES5. It lacked support for most
ES6 features. Microsoft ended the support for Internet Explorer in 2022.

Early Versions (ES1 → ES5)

ES1 (1997) :- First official version

ES2 (1998) :- Minor updates

ES3 (1999) :- Added,Regular expressions ,Better string handling

A major update came with ES6 (2015), which introduced modern features like
classes, arrow functions, modules, and promises. After that, JavaScript evolved
through yearly updates (ES2016 onwards), adding features like async/await and
optional chaining.

With the introduction of [Link], JavaScript expanded beyond browsers and is


now widely used for frontend, backend, and mobile application development.
JavaScript Operators
Operators are for Mathematical and Logical Computations

The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator > compares values

Types of Operators:

1. Arithmetic Operators: +, -, *, /, %, ++, -- – for calculations.


2. Assignment Operators: =, +=, -=, *=, /= – to assign or update values.
3. Comparison Operators: ==, ===, !=, >, <, >=, <= – to compare values.
4. Logical Operators: &&, ||, ! – to combine or invert conditions.
5. Ternary Operator: condition ? value1 : value2 – shorthand for if-else.
6. Type Operators: typeof, instanceof – to check type or object.

Operators are used in JavaScript for calculations, comparisons, decision making, and type
checking.

1. Arithmetic Operators
Operator Meaning Example

+ Addition 5 + 2 = 7

- Subtraction 5 - 2 = 3

* Multiplication 5 * 2 = 10

/ Division 10 / 2 = 5

% Modulus (remainder) 5 % 2 = 1

** Exponentiation 2 ** 3 = 8
2. Assignment Operators

Used to assign values to variables.

Operator Example Same As

= x = 5 —

x = x +
+= x += 2
2

x = x -
-= x -= 2
2

x = x *
*= x *= 2
2

x = x /
/= x /= 2
2

3. Comparison Operators

Used to compare values (returns true or false).

Operator Description
== Equal (loose)
=== Strict equal (value + type)
!= Not equal
!== Strict not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal

4. Logical Operators
Used to combine conditions.
Operator Description
&& AND
` OR
! NOT
Control statements in javascript
JavaScript control statement is used to control the execution of a program
based on a specific condition. If the condition meets then a particular block of
action will be executed otherwise it will execute another block of action that
satisfies that particular condition.

Types of Control Statements in JavaScript


Conditional Statement: These statements are used for decision-making, a
decision. n is made by the conditional statement based on an expression that is
passed. Either YES or NO.
Iterative Statement: This is a statement that iterates repeatedly until a
condition is met. Simply said, if we have an expression, the statement will keep
repeating itself until and unless it is satisfied.

1. If Statement
In this approach, we are using an if statement to check a specific condition, the
code block gets executed when the given condition is satisfied.

Syntax
if ( condition_is_given_here )
{
// If the condition is met,
//the code will get executed. }

2. Using If-Else Statement


The if-else statement will perform some action for a specific condition. If the condition meets then a
particular code of action will be executed otherwise it will execute another code of action that
satisfies that particular condition.

Syntax
if (condition1) {

// Executes when condition1 is true

if (condition2) {

// Executes when condition2 is true } }


3. Using Switch Statement
The switch case statement in JavaScript is also used for decision-making
purposes. In some cases, using the switch case statement is seen to be more
convenient than if-else statements.

Syntax
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
case valueN:
statementN;
break;
default:
statementDefault;
}

Using For loop


In this approach, we are using for loop in which the execution of a set of
instructions repeatedly until some condition evaluates and becomes false
Syntax
for (statement 1; statement 2; statement 3)
{
// Code here . . .
}

Using While loop


The while loop repeats a block of code as long as a specified condition is true.

Syntax
while (condition) {
// code block
}

Using Do-While loop


The do-while loop is similar to the while loop, except that the condition is
evaluated after the execution of the loop's body. This means the code block will
execute at least once, even if the condition is false.

Syntax
do {
// code block
}
while (condition);
Difference between javascript and VB script
JavaScript
JavaScript is a lightweight and object-oriented scripting language used to create dynamic
HTML pages with interactive effects within a webpage. It is an interpreted scripting language
and its code is run in a web browser only. It is also called a browser’s language and can be
used for client-side developments as well as server-side developments. It was developed by
Brendan Eich at Netscape and was first released in 1995.

VBScript
VBScript is an active scripting language introduced by Microsoft. It is developed based on
Visual Basic and is also called a Visual Basic scripting language. It does not support all
browsers, supports only Internet Explorer. It is generally used in Microsoft environments and
uses a component object model to access system objects and variables of the environment.

JavaScript VBScript
Netscape Microsoft
Works on all browsers (Chrome, Edge, Firefox,
Mainly works only on Internet Explorer
etc.)
Deprecated / no longer used in modern
Widely used and actively developed
web
C-like syntax Visual Basic-like syntax
Case-sensitive Not case-sensitive
Web development (frontend + backend via
Old web pages, Windows scripting
[Link])
Faster, optimized by modern engines Slower compared to JavaScript
Strong support for events Limited support
Huge global community Very limited now
Essential skill today Obsolete technology
Difference between javascript and java
JavaScript Java
Netscape Sun Microsystems
Scripting language Programming language
Runs in browser / runtime Runs on JVM (Java Virtual Machine)
Interpreted (just-in-time compiled) Compiled to bytecode
Flexible, dynamic Strict, object-oriented
Dynamically typed Statically typed
Mainly web (frontend + backend) Cross-platform applications
Web development, apps, APIs Enterprise apps, Android apps
Fast for web tasks Generally faster for large apps
.js .java

Arrays in javascript
Arrays in JavaScript are used to store multiple values in a single variable. Instead of
creating many variables, you can keep related data together in an array. An array is a list-like
structure that holds values (elements) in an ordered way. An Array is an object type
designed for storing data collections.

Key characteristics of JavaScript arrays are:

 Elements: An array is a list of values, known as elements.

 Ordered: Array elements are ordered based on their index.

 Zero indexed: The first element is at index 0, the second at index 1, and so
on.

 Dynamic size: Arrays can grow or shrink as elements are added or removed.

 Heterogeneous: Arrays can store elements of different data types (numbers,


strings, objects and other arrays).
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.

Syntax:
const array_name = [item1, item2, ...];

Example
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p id="demo"></p>
<script>
const cars = [
"Saab",
"Volvo",
"BMW"
];
[Link]("demo").innerHTML = cars;
</script>
</body>
</html>
Functions in JavaScript
Functions in JavaScript are reusable blocks of code designed to perform a specific task. They help
make programs organized, reusable, and easier to maintain. A function is a set of statements that
runs when it is called.

 Functions are reusable code blocks designed for particular tasks.


 Functions are executed when they are called or invoked.
 Functions are fundamental in all programming languages.

Function Declarations

Earlier in this tutorial, you learned that functions are declared with the following
syntax:

function functionName(parameters) {
// code to be executed
}

A function declaration uses the function keyword and a function name.

Example
<!DOCTYPE html>

<html>

<body>

<h1>JavaScript Functions</h1>

<h2>The return Statement</h2>

<p>A function that performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>

function myFunction(a, b) {return a * b}

let x = myFunction(4, 3);

[Link]("demo").innerHTML = "The product is " + x;

</script>

</body>

</html>
Standard date time function in javascript

The JavaScript Date object represents a specific moment in time, measured in


milliseconds since the Unix Epoch (January 1, 1970). It's crucial for working
with date and time in web applications, providing methods for tasks like date
manipulation, formatting, and calculations.
What is the JavaScript Date Object?
In JavaScript, the Date object tracks time as the number of milliseconds since the Unix
Epoch. You can create a Date object using the new Date() constructor, which allows you to
specify either the current date and time or a particular date.

Creating Date Objects

let now = new Date(); // current date & time


let specific = new Date("2025-01-01");
let custom = new Date(2025, 0, 1); // (year, month, day)

Method Purpose
getFullYear() Get year
getMonth() Get month
getDate() Get day
getHours() Get hours
setFullYear() Set year
toLocaleString() Format date

Getting Date & Time Values

let d = new Date();


[Link](); // year (e.g., 2026)
[Link](); // month (0-11)
[Link](); // day of month (1-31)
[Link](); // day of week (0-6)
[Link](); // hours (0-23)
[Link](); // minutes (0-59)
[Link](); // seconds (0-59)
[Link](); // milliseconds
Example (Age Calculator)
<!DOCTYPE html>
<html>
<body>
<h2>Age Calculator</h2>
Enter Birth Year: <input type="number" id="year">
<button ><p id="result"></p>
<script>
function calculateAge() {
let birthYear = [Link]("year").value;
let currentYear = new Date().getFullYear();
let age = currentYear - birthYear;
[Link]("result").innerHTML =
"Your age is " + age;}
</script>
</body>
</html>

You might also like