lecture_01
lecture_01
LECTURE-1
Introduction:
JavaScript (js) is a light-weight object-oriented programming language which is used by several
websites for scripting the WebPages.
It is written in client side and execute on the client browser which reduce load on server.
JavaScript is a client-side scripting language (though it can also be used server-side with tools like Node.js).
Ceating of
Features:
Interpreted Language
Minimal memory usage and quick execution, ideal for web applications.
Dynamic Typing
You don't need to declare variable types (e.g., let x = 10; vs. int x = 10; in Java).
Object-Oriented (Prototype-Based)
Supports object creation and inheritance using prototypes rather than classical classes .
First-Class Functions
Functions are treated like variables. You can pass them as arguments, return them, and assign them to variables.
Event-Driven Programming
Commonly used for handling user interactions like clicks, mouse movements, keypresses, etc.
Uses event loop and callback functions to handle asynchronous operations like AJAX, fetch, and setTimeout.
Browser Compatibility
Supported by all major web browsers (Chrome, Firefox, Safari, Edge, etc.) without the need for plugins.
Client-Side and Server-Side Usage
Primarily used on the client side but can also be used server-side with platforms like Node.js.
Cross-Platform
Can run on different platforms, including desktops, servers, mobile devices, and even IoT devices.
Applications of JavaScript
Web Development (Front-End)
Web Development (Back-End)
Mobile App Development
Desktop Application Development
Game Development
Data Visualization
Machine Learning (in the Browser)
Automation & Scripting
Internet of Things (IoT)
Progressive Web Apps (PWAs)
Another way to execute javascript is a runtime like node.js which can be installed and used to run javascriipt code,
syntax
<script>
// java script code
</script>
Example:
<html>
<body>
<script type="text/javascript">
<!-- java script code can be embeded inside body section -->
document.write("This is my first program abut java script");
</script>
</body>
</html>
Here <script> tag is used to including the script into html document
The document.write command is a standard JavaScript command for writing output to a page.
By entering the document.write command between the <script> and </script> tags, the browser will
recognize it as a JavaScript command and execute the code line.
Java script statement:
document.write("welcome");
JavaScript Code
JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
Each statement is executed by the browser in the sequence they are written.
Example:
<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
JavaScript Blocks
JavaScript statements can be grouped together in blocks.
Blocks start with a left curly bracket {, and ends with a right curly bracket }.
The purpose of a block is to make the sequence of statements execute together.
Example:
<script type="text/javascript">
{
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
}
</script>
Link JavaScript File in HTML
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 HTML file in which we want to add this code.
Example:
<html>
<head>
<script type="text/javascript" src="abc.js"></script>
</head>
<body>
</body>
</html>
Syntax:
document.getElementsByClassName("test")[0];
EXAMPLE
Example:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerText = "Hello World";
</script>
</body>
</html>
Using window.alert().
You can use an alert box to display data. Here window keyword is optional.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Output
Java script comment
It is a non executable statement which is not executed by the compiler. It Simply ignored by the compiler.
Example:
<script type="text/javascript">
// Write a heading
document.write("<h1>This is a heading</h1>");
// Write two paragraphs:
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
2. Multi line comments. Starts with start with /* and end with */.
Example:
<script type="text/javascript">
/*
The code below will write
one heading and two paragraphs
*/
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
Variable:
Variables are "containers" for storing information. JavaScript variables are used to hold values or expressions.
In java script variables are used to hold values or expressions. The values of the variables are allocated using
assignment operator (“=”)
Variables are case-sensitive in Javascript. This means that schoolName and schoolname are considered
different variables.
We can use letters, digits, symbols like dollar sign ($) and underscore ( _ ) in a variable name.
We cannot use any of the reserved keywords (function, return, typeof, break, etc.) of Javascript as a variable
name.
Declaration of variable
JavaScript variable can be declared in 4 ways
1. Automatically
2. Using var
3. Using let
4. Using const
Automatically
JavaScript allows the automatic or implicit declaration of variables by assigning a value to a name without
using var, let, or const.
Syntax:
myNumber = 10;
Using var
Syntax:
var s=5;
Using let
Syntax:
Using const
const is utilized for defining variables with values that are intended to remain constant and unchangeable.
Syntax:
The constant must be initialized when you declare it, otherwise, it will throw a " SyntaxError: Missing
initializer in const declaration ".
Once assigned a value to a constant, you can’t re-assign values afterward. That is, a value is fixed to a
constant
Example 1:
var ages; //we can declare a variable without initializing it.
const age; //It will throw an error as we haven't initialized the constant.
Example 2:
const pi = 3.142;
Note: Though we cannot change the value of the constant, but if the constant value is an object, then we
can modify the object's properties.
Example 3:
//The above line will throw an error, because we are trying to reassign
car.company = 'Audi';
Boolean This data type can accept only two values i.e. true and false.
let x = true;
let y = false;
Undefined variable has been declared but has not been assigned a value .
let x;
let x = null;
Bigint Represents integers of arbitrary precision, exceeding the limits of the number type
symbol Symbol data type is used to create objects which will always be unique.
Non primitive data type: Non-primitive data types in JavaScript, also known as reference types, can
hold collections of values and more complex entities.
Object: Objects are collections of key-value pairs, where keys are strings (or symbols) and values can be of any
data type, including other objects.
Example:
let person = { name: "John Doe", age: 30, city: "New York" };
Array: Arrays are ordered lists of values. Each value in an array is called an element, and elements can be of
any data type.
Example:
let numbers = [1, 2, 3, 4, 5];
let mixedArray = [1, "hello", true, null];
Function: Functions are blocks of reusable code that perform specific tasks.
Example:
function add(a, b) {
return a + b;
}
Date: The Date object is used to work with dates and times. It provides methods for creating, manipulating, and
formatting dates.
Example:
let now = new Date();
let christmas = new Date(2025, 11, 25); // Month is 0-indexed
RegExp: Regular expressions are used to define search patterns for matching text in strings. They are objects
that can be used to search, replace, and validate text.
Example:
let pattern = /hello/i; // Case-insensitive match for "hello"
let result = pattern.test("Hello world"); // true
Map: The Map object holds key-value pairs where keys can be any data type. It remembers the insertion order
of entries.
Example:
const myMap = new Map();
myMap.set('a', 1);
myMap.set('b', 2);
Set: The Set object lets you store unique values of any type, whether primitive values or object references.
Example:
const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(2); // Only one 2 will be stored
Types of operator
JavaScript supports the following types of operators.
1. Arithmetic Operators
2. Comparison Operators
3. Logical (or Relational) Operators
4. Assignment Operators
5. Conditional (or ternary) Operators
6. Bitwise operator
Arithmetic Operators
Example:
<html>
<body>
<script type="text/javascript">
var a = 5;
var b = 2;
var c = "Test";
var linebreak = "<br />";
document.write("a + b = ");
result = a + b;
document.write(result);
document.write(linebreak);
document.write(linebreak);
document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);
document.write(linebreak);
document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);
document.write(linebreak);
document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);
document.write(linebreak);
document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);
document.write(linebreak);
a = a++;
document.write("a++ = ");
result = a++;
document.write(result);
document.write(linebreak);
document.write(linebreak);
b = b--;
document.write("b-- = ");
result = b--;
document.write(result);
document.write(linebreak);
document.write(linebreak);
</script>
<p>Set the variables to different values and then try...</p>
</body>
</html>
Comparison operator
Comparison operators in JavaScript are used to compare two values and return a Boolean value
(true or false).
Assignment operator
Assignment operators assign values to JavaScript variables.
Ternary operator:
A ternary operator evaluates a condition and executes a block of code based on the condition.
Syntax:
Condition? expression1:expression2
Bitwise operator
Bitwise operators treat its operands as a set of 32-bit binary digits (zeros and ones) and perform
actions.