Javascript_class12
Javascript_class12
Q 1 What is javaScript ?
Ans:
1. JavaScript is a cross-platform, object-oriented scripting language.
2. JavaScript is a small, lightweight language. It is not useful as a standalone language, but is
designed for easy embedding in other products and applications, such as web browsers.
3. Inside a host environment, JavaScript can be connected to the objects of its environment to
provide programmatic control over them.
Q.2 What is Difference between client side java script and server side java script?
Ans:
Client-side JavaScript is a type of programming that runs inside your web browser (like
Chrome or Firefox) to make web pages interactive and fun. It works right on your computer
without needing to ask a server for help every time something happens.
For example, client-side extensions allow an application to place elements on an HTML form
and respond to user events such as mouse clicks, form input, and page navigation.
Server-side JavaScript extends the core language by supplying objects relevant to running
JavaScript on a server.
Example:
<script language="text/javascript">
document.write("<h1>Main Title</h1>");
document.write("<p align='right'>Body</p>");
</script>
Example:
<html>
<head>
<title>JavaScript Arithmetic Operators</title>
</head>
<body>
<script language="JavaScript">
var x = 10;
var y = 4;
document.write("Addition=",x + y);
document.write("<br>");
document.write("Subtraction=",x - y);
document.write("<br>");
document.write("Multiplication=",x * y);
document.write("<br>");
document.write("Division=",x / y);
document.write("<br>");
document.write("Remainder=",x % y);
</script>
</body>
</html>
Example:
<html>
<head>
<title>JavaScript Comparison Operators</title>
</head> <body>
<script language="JavaScript">
var x = 25;
var y = 35;
var z = "25";
document.write(x == z);
document.write("<br>");
document.write(x === z);
document.write("<br>");
document.write(x != y);
document.write("<br>");
document.write(x !== z);
document.write("<br>");
document.write(x < y);
document.write("<br>");
document.write(x > y);
document.write("<br>");
document.write(x <= y);
document.write("<br>");
document.write(x >= y);
</script>
</body>
</html>
Example:
<html>
<head>
<title>JavaScript Logical Operators</title>
</head>
<body>
<script language="JavaScript">
var year = 2018;
if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)))
{
document.write(year + " is a leap year.");
}
else
{
document.write(year + " is not a leap year.");
}
</script>
</body>
</html>
Example:
<html >
<head>
<title>JavaScript Assignment Operators</title>
</head>
<body>
<script language="JavaScript">
var x; // Declaring Variable
x = 10;
document.write(x + "<br>");
x = 20;
x += 30;
document.write(x + "<br>");
x = 50;
x -= 20;
document.write(x + "<br>");
x = 5;
x *= 25;
document.write(x + "<br>");
x = 50;
x /= 10;
document.write(x + "<br>");
x = 100;
x %= 15;
document.write(x);
</script>
</body>
</html>
Example:
var age=20;
var allowed = (age > 18) ? "yes" : "no";
Q.9. What are Data Types in JavaScript?
Ans:
A data type is a classification of the type of data that a variable or object can hold. Data type is
an important factor in virtually all computer programming languages, including visual basic, C#,
C/C++ and JavaScript.
Different data types are Numbers, String, Boolean, undefined
Example:
boolean(" ")
false
boolean(234)
true
Example:
var name = "kittens";
if (name == "puppies")
{
name += "!";
}
else if
(name == "kittens")
{
name += "!!";
}
else
{
name = "!" + name;
}
Q.16. Give the difference between while loop and do-while loop in Javascript.
JavaScript has while loops and do-while loops. While loop is good for basic looping. It is called
as entry controlled loop. The do-while loop is used to ensure that the body of the loop is
executed at least once. It is called as exit controlled loop.
switch(+number) {
case 1:
alert("Number is One!");
break;
case 2:
alert("Number is Two!");
break;
case 3:
alert("Number is Three!");
break;
default:
alert("Number is not 1, 2, or 3!");
}
</script>
</body>
</html>
Functions makes the code much easier to maintain — Since a function created once can be
used many times, so any changes made inside a function automatically implemented at all the
places without touching the several files.
Functions makes it easier to eliminate the errors — When the program is subdivided into
functions, if any error occurs you know exactly what function causing the error and where to find
it. Therefore, fixing errors becomes much easier.
Example:
<html>
<head>
<title>JavaScript Define and Call a Function</title>
</head>
<body>
<script>
// Defining function
function Hello()
{
document.write("Hello, welcome to this website!");
}
// Calling function
Hello(); // Prints: Hello, welcome to this website!
</script>
</body>
</html>
Q.22. What are String Objects? Explain the methods used in String function.
Strings in JavaScript are sequences of characters.
Syntax: var name=”Student_name”
The String object provides methods and properties for string manipulation and formatting.
Syntax: stringName.method()
ii. The strings are represented using objects and they have methods as well:
"hello, world".replace("hello", "goodbye") //by using replace() method
goodbye, world
//Program to find the position of the first occurrence of a text in a string using indexOf()
<html>
<body>
<p id="demo"><strong>click the button to locate where in the string a specified value
occurs.</strong></p>
<button onclick="myFunction()">try it</button>
<script>
function myFunction()
{
var str="hello! welcome to my world";
var n=str.indexOf("world");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
//Program to search for a text in a string and return the text if found using match()
<html>
<body>
<script>
var str="Honesty is the best policy";
document.write(str.match("policy")+"<br>");
document.write(str.match("Police")+"<br>");
document.write(str.match("pollicy")+"<br>");
document.write(str.match("policy")+"<br>");
</script>
</body>
</html>
Output:
Policy
Null
Null
Policy
round() method: The round() method returns a number to the nearest integer.
Syntax: Math.round()
//Program to round off any number using round()
<html>
<body>
<p id="demo">click the button to round the no.to its nearest integer.</p>
<button onclick="myFunction()">try it</button>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML=Math.round(8.7896);
}
</script>
</body>
</html>
random(): The random() method returns a random number from 0(inclusive) up to but not
including 1(exclusive).
Syntax: Math.random()
max(): The max() method returns the number with the highest value.
Syntax: Math.max(n1,n2,n3……..nx)
//Program to return the number with highest value of two specified numbers using max()
<html>
<body>
<p id="demo">Click the button to return the highest no. between 5 and 10.</p>
<button onclick="myFunction()">try it</button>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML=Math.max(5,10);
}
</script>
</body>
</html>
min() The min() method returns the number with the lowest value.
Syntax: Math.min(n1,n2,n3……….nx)
//Program to return the number with the lowest value of two specified number using min().
<html>
<body>
<p id="demo">Click the button to return the lowest no. between 77 and 9.</p>
<button onclick="myFunction()">try it</button>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML=Math.min(77,9);
}
</script>
</body>
</html>
<html>
<head>
<title>Creating Arrays in JavaScript</title>
</head>
<body>
<script>
// Creating variables
var colors = ["Red", "Green", "Blue"];
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
var cities = ["London", "Paris", "New York"];
var person = ["John", "Wick", 32];
Output: 3
Example 3:
var a = ["dog", "cat", "hen"];
a[100] = "fox";
document.write(a.length);
document.write(typeof a[90]);
Output:
101
Undefined
//Program to remove the last element from the array by using pop()
<html>
<body>
<p id="demo">Click the button to remove the last array element.</p>
<button onclick="myFunction()">Click me</button>
<script>
var name=["John","Mary","Oliver","Alice"];
function myFunction()
{
name.pop();
var x=document.getElementById("demo");
x.innerHTML=name;
}
</script>
</body>
</html>
Additional Programs:
<html>
<head>
<title>JavaScript Code</title>
</head>
<body>
<h1>Age Checker</h1>
<p>Enter your age:</p>
<input type="number" id="ageInput" placeholder="Enter your age" />
<button onclick="checkAge()">Check</button>
<script>
function checkAge()
{
const age = document.getElementById("ageInput").value;
// Conditional statements
if (age < 18) {
document.write("You are a minor.");
} else if (age >= 18 && age < 65) {
document.write("You are an adult.");
} else if (age >= 65) {
document.write("You are a senior citizen.");
} else {
document.write("Please enter a valid age.");
}
}
</script>
</body>
</html>