Professional Documents
Culture Documents
Javasccript Background Theory
Javasccript Background Theory
JavaScript
Q2. Write difference between Client Side Scripting and Server Side
Scripting.
Client-Side Scripting:
Execution: Runs on the user's browser.
Responsibility: Deals with the user interface and client-side interactions.
Processing: The client's device processes the scripts.
Examples: JavaScript is a common client-side scripting language.
Server-Side Scripting:
Execution: Runs on the server.
Responsibility: Manages server operations, database interactions, and
business logic.
Processing: The server processes the scripts and sends the results to the
client.
Examples: PHP, Python (Django), Ruby (Ruby on Rails) are server-side
scripting languages.
Logical Operators:
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
Assignment Operators:
= (Assignment)
+= (Add and assign)
`-= (Subtract and assign)
*= (Multiply and assign)
/= (Divide and assign)
%= (Modulus and assign)
Unary Operators:
++ (Increment)
-- (Decrement)
typeof (Typeof)
! (Logical NOT)
Server-Side JavaScript:
Uses JavaScript on the server to handle server-side operations.
Common in environments like Node.js, where JavaScript is used to build
scalable and high-performance server applications.
Mobile JavaScript:
Used in frameworks like React Native and Ionic to build mobile
applications.
Enables the development of cross-platform mobile apps using
JavaScript.
5. To input first name and last name using prompt box and display
the full name.
<!DOCTYPE html>
<html lang="en">
<head><title>Display Name</title>
<script>
var firstName = prompt("Enter your first name:");
var lastName = prompt("Enter your last name:");
alert("Full Name: " + firstName + " " + lastName);
</script>
</head>
<body>
</body>
</html>
17. To input a number and print the square and cube of a number
<!DOCTYPE html>
<html lang="en">
<head> <title>Square and Cube Calculation</title>
<script>
function calculateSquareAndCube() {
var num = parseFloat(prompt("Enter a number:"));
var square = num * num;
var cube = num * num * num;
alert("Square: " + square + "\nCube: " + cube);
}
</script>
</head>
<body>
<button onclick="calculateSquareAndCube()">Calculate Square
and Cube</button>
</body>
</html>
JavaScript
Arrays, objects,
classes
21. To show
how to use Array () object in JavaScript.
<!DOCTYPE html>
<html lang="en">
<head><title>Array Example</title>
<script>
var fruits = new Array("Apple", "Banana", "Orange");
alert("Fruits: " + fruits.join(", "));
</script>
</head>
<body></body>
</html>
23. Displaying the sum of two input numbers in text box using
document.getElementById in JavaScript.
<!DOCTYPE html>
<html lang="en">
<head> <title>Sum Display</title>
<script>
function displaySum() {
var num1 = parseFloat(document.getElementById("num1").value)
|| 0;
var num2 = parseFloat(document.getElementById("num2").value)
|| 0;
var sum = num1 + num2;
alert("Sum: " + sum);
}
</script>
</head>
<body>
<label for="num1">Number 1:</label>
<input type="text" id="num1" /><br>
<label for="num2">Number 2:</label>
<input type="text" id="num2" /><br>
<button onclick="displaySum()">Calculate Sum</button>
</body>
</html>
24. Input first
name and last
name of a
student using a
text box and displaying full name using document.getElementById
in JavaScript.
<!DOCTYPE html>
<html lang="en">
<head> <title>Student Name Display</title>
<script>
function displayFullName() {
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var fullName = firstName + " " + lastName;
alert("Full Name: " + fullName);
}
</script>
</head>
<body>
<label for="firstName">First Name:</label>
<input type="text" id="firstName" /><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" /><br>
<button onclick="displayFullName()">Display Full
Name</button>
</body>
</html>
25. To
show the
use of
JavaScript class and object
<!DOCTYPE html>
<html lang="en">
<head><title>Class and Object Example</title>
<script>
class Person {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
getInfo() {
return this.firstName + " " + this.lastName + ", Age: " + this.age;
}
}
var person = new Person("Zayed", "Ahmad", 18);
alert("Person Info: " + person.getInfo());
</script>
</head>
<body>
</body>
</html>
26. To show the use of different jQuery functions
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function() {
$("#result").text("Hello, jQuery!");
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
27. Input a number and print its square and cube using
document.getElementById in JavaScript.
<!DOCTYPE html>
<html lang="en">
<head><title>Square and Cube Display</title>
<script>
function displaySquareAndCube() {
var
num=parseFloat(document.getElementById("userInput").value) ||
0;
var square = num * num;
var cube = num * num * num;
alert("Square: " + square + "\nCube: " + cube);
}
</script>
</head>
<body>
<label for="userInput">Enter a number:</label>
<input type="text" id="userInput" /><br>
<button onclick="displaySquareAndCube()">Calculate Square and
Cube</button><br>
</body>
</html>
28. Write a JavaScript code to validate a “login form”.
Example of code and output of the program
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS</title>
</head>
<body>
first number:<input type="text" id="fn"><br><br>
second number:<input type="text" id="sn"><br><br>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
let x = +(document.getElementById("fn").value);
let y = +(document.getElementById("sn").value);
document.getElementById("demo").innerHTML = x+y;
}
</
script>
</body>
</html>