JavaScript Detailed Basic Notes (B.
Tech 1st Year)
1. Introduction to JavaScript
JavaScript is a high-level scripting language used to create interactive and dynamic web pages.
Websites generally use three technologies:
HTML – structure of webpage
CSS – styling and design
JavaScript – functionality and interaction
Example:
<script>
[Link]("Hello Students");
</script>
2. Ways to Add JavaScript in HTML
There are three methods to include JavaScript in webpages.
Internal JavaScript:
Code written inside HTML file within script tags.
External JavaScript:
JavaScript written in a separate file with .js extension and linked to HTML.
Inline JavaScript:
JavaScript written directly inside HTML elements.
Example:
<button Me</button>
3. JavaScript Output Methods
JavaScript can display output using several methods.
[Link]() – displays output directly on webpage.
[Link]() – prints output in browser console used for debugging.
alert() – shows popup message box.
prompt() – takes input from user.
confirm() – shows dialog box with OK and Cancel options.
4. Variables in JavaScript
Variables are used to store data values.
Example:
let age = 20;
Types of variable declarations:
var – older method used in early JavaScript.
let – modern variable declaration used in most programs.
const – used to declare constant values that cannot be changed.
Example:
const pi = 3.14;
5. Data Types
Data types define the type of value stored in variables.
Common JavaScript data types:
Number – example: 25
String – example: "Hello"
Boolean – true or false
Array – example: [1,2,3]
Object – example: {name:"John"}
Example:
let name = "Rahul"
let age = 21
let pass = true
6. Operators
Operators perform mathematical or logical operations.
Arithmetic Operators:
+ addition
- subtraction
* multiplication
/ division
% modulus
Comparison Operators:
== equal
=== strict equal
!= not equal
> greater than
< less than
Example:
let a = 10
let b = 5
let result = a + b
7. Conditional Statements
Conditional statements help in decision making.
if statement:
Executes block of code if condition is true.
Example:
if(age >= 18)
{
[Link]("You can vote")
}
if-else statement:
Used when program must choose between two options.
switch statement:
Used when many conditions need to be checked.
8. Loops
Loops are used to repeat code multiple times.
for loop:
Used when number of repetitions is known.
Example:
for(let i=1;i<=5;i++)
{
[Link](i)
}
while loop:
Runs while condition remains true.
do-while loop:
Executes code at least once before checking condition.
9. Functions
Functions are reusable blocks of code used to perform specific tasks.
Example:
function greet()
{
[Link]("Hello Students")
}
greet()
Function with parameters:
function add(a,b)
{
return a+b
}
let result = add(5,3)
10. Arrays
Arrays store multiple values in a single variable.
Example:
let fruits = ["Apple","Mango","Banana"]
Accessing array element:
fruits[1]
11. Events in JavaScript
Events occur when user interacts with webpage.
Common events:
onclick – triggered when button is clicked
onmouseover – triggered when mouse pointer moves over element
onload – triggered when page finishes loading
Example:
<button >
12. DOM (Document Object Model)
DOM allows JavaScript to change HTML content dynamically.
Example:
[Link]("demo").innerHTML = "Welcome Students"
13. eval() Function
The eval() function evaluates a string as JavaScript code.
Example:
let x = "10 + 5"
let result = eval(x)
14. Comments in JavaScript
Comments are used to explain code and make it easier to understand.
Single line comment:
// This is a comment
Multi line comment:
/*
This is
multi-line comment
*/