Unit1 3
Unit1 3
Inline JavaScript
You can write JavaScript code directly inside the HTML element using
the onclick, onmouseover, or other event handler attributes.
<html>
<head></head>
<body>
<h2>
Adding JavaScript in HTML Document
</h2>
<button onclick="alert('Button Clicked!')">
Click Here
</button>
</body>
</html>
Internal JavaScript (Within <script> Tag)
You can write JavaScript code inside the <script> tag within the HTML file. This is known
as internal JavaScript and is commonly placed inside the <head> or <body> section of
the HTML document.
1. JavaScript Code Inside <head> Tag
Placing JavaScript within the <head> section of an HTML document ensures that the
script is loaded and executed as the page loads. This is useful for scripts that need to be
initialized before the page content is rendered.
<body>
<html> <h2>
<head> Add JavaScript Code
<script> inside Head Section
function myFun() { </h2>
document.getElementById("demo") <h3 id="demo" style="color:green;">
.innerHTML = "Content changed!"; Computer Multimedia and animation
} </h3>
</script> <button type="button" onclick="myFun()">
</head> Click Here
</button>
</body>
</html>
2. JavaScript Code Inside <body> Tag
JavaScript can also be placed inside the <body> section of an HTML page.
Typically, scripts placed at the end of the <body> load after the content,
which can be useful if your script depends on the DOM being fully loaded.
</h3>
External JavaScript (Using External File)
For larger projects or when reusing scripts across multiple HTML files, you can place your JavaScript code in an external
.js file. This file is then linked to your HTML document using the src attribute within a <script> tag.
<html>
<head> /* Filename: script.js*/
<script src="script.js"></script>
function myFun () {
</head> document.getElementById('demo')
<body> .innerHTML = 'Content Changed'
<h2> }
External JavaScript
</h2>
<h3 id="demo" style="color:green;">
Computer Multimedia and animation
</h3>
<button type="button" onclick="myFun()">
Click Here
</button>
</body>
</html>
Advantages of External JavaScript
•Faster Page Load Times: Cached external JavaScript files don’t need
to be reloaded every time the page is visited, which can speed up
loading times.
•Improved Readability and Maintenance: Keeping HTML and
JavaScript separate makes both easier to read and maintain.
•Separation of Concerns: By separating HTML (structure) and
JavaScript (behavior), your code becomes cleaner and more modular.
•Code Reusability: One external JavaScript file can be linked to
multiple HTML files, reducing redundancy and making updates easier.
JavaScript Syntax
Syntax
// Variable declaration
let c, d, e;
JavaScript Literals
console.log(num1)
Output:
console.log(num2)
console.log(str1) 50
50.05
console.log(str2) bca
CMA
Java Script Primitives
Primitive and Non-primitive data-types in JavaScript
output
Value of str=Hello All
Value of str1=Welcome to my new house
Undefined:
This means that a variable has been declared but has not been assigned a
value, or it has been explicitly set to the value `undefined`.
let x;
console.log(x); // Outputs: undefined
Boolean:
The boolean data type can accept only two values i.e. true and
false.
let x;
console.log(x); // Outputs: undefined
Null:
This data type can hold only one possible value that is null.
let x = null;
console.log("Value of x=" + x); Output
Value of x=null
BigInt:
BigInt data type can represent numbers greater than 253-1 which helps to perform
operations on large numbers. The number is specified by writing ‘n’ at the end of
the value
let bigNum = 123422222222222222222222222222222222222n
console.log(bigNum)
Output
123422222222222222222222222222222222222n
Symbol:
Output
symbol Symbol(Hello)
2.Non-primitive Data Types
Non-primitive data types, also known as reference types,
are objects and derived data types. They can store
collections of values or more complex entities. The two
key non-primitive data types in JavaScript are:
Below is a list of Non-primitive data types.
Object:
An object in Javascript is an entity having properties and methods.
Everything is an object in javascript.
How to create an object in javascript:
•Using Constructor Function to define an object:
// Create an empty generic object
let obj = new Object();
Output
Shiv Shankar
Array:
With the help of an array, we can store more than one
element under a single name.
Ways to declare a single-dimensional array:
// Call it with no arguments
let a = new Array();
Primitive Non-Primitive
Non-Primitive data types
Primitive Data types are
are created by the
predefined.
programmer