Ch - 6 Advanced web publishing (Javascript)
Q 1) What do you mean by Computer Network? Explain.
A computer network is a collection of interconnected computers and other devices (such as printers and routers) that are linked together to share resources and information. These networks can be small, like a home network connecting a few devices, or large, like the global internet, which connects billions of devices worldwide. Computer networks enable communication and data exchange between devices, facilitating the sharing of data, resources, and services.
Advantages of Computer Networks:
- Central storage of data: Files are stored on a central database that can be accessed by everyone.
- Connectivity: A single connection can be routed to connect multiple devices.
- Communication: Computers can communicate with each other through email, chat, video conferencing, etc.
- Resource sharing: Computers can share resources such as printers, scanners, internet, etc.
- Cost factor: Computer networks can reduce the cost of hardware and software by sharing them among users.
- Remote Access: Users can access resources and data on the network from remote locations, enhancing flexibility and productivity, especially in the context of remote work and online learning.
- Security: Properly configured networks can have robust security measures in place to protect against unauthorized access and data breaches.
Q 2) Difference between LAN and WAN?
| LAN | WAN |
|---|---|
| LAN is a group of devices connected in a small geographic area, such as houses, offices, or buildings. | WAN is an arrangement of several devices attached over a network covering a broad area. A network having communication links crossing the regional, metropolitan, or national boundaries over a large distance is an example of WAN. |
| LAN’s are often owned and controlled by a single organization, such as a company, school, or home network. | WANs may involve multiple organizations, service providers, and can be more complex to manage due to their wide-reaching nature. |
| LAN covers a small geographical area, and it does not require any leased telecommunication lines. | WAN covers a large distance geographical area that usually crosses regional or metropolitan boundaries and requires leased telecommunication lines. |
| The configuration and maintenance is easy in LAN. | The configuration and maintenance is harder than LAN. |
| LAN provides a comparatively higher speed. | WAN has a slower speed as compared to LAN. |
| Transmission medium used in LAN is co-axial or UTP cable. | Whereas WAN uses PSTN or satellite link as a transmission or communication medium. |
| LAN Examples -: Building Network, Office Network, Campus Network. | WAN Examples -: Internet, Corporate Wide Area Network, Cellular Network. |
Q 3) Explain TCP/IP Suite?
TCP/IP, is a set of communication protocols that govern how data is transmitted, routed, and received over the Internet and many other computer networks. It is the foundation of the modern internet and plays a crucial role in ensuring data communication between devices across the globe.
TCP/IP consists of four layers: Application, Transport, Internet, and Network Access layer. Each layer has a specific function and uses different protocols to perform it.
- Network Access Layer -: it is the first layer of the four layer TCP/IP model. This layer provides access to the physical network media, such as Ethernet cables, wireless signals, optical fibers, etc. Some protocols in this layer are Ethernet, Wi-Fi, Bluetooth, etc.
- Internet Layer :- it is the second layer of the four layer TCP/IP Model. The position of Internet layer is between Network Access Layer and Transport Layer. This layer provides routing and addressing of data packets across networks, such as assigning IP addresses, forwarding packets, resolving host names, etc. The primary protocol at this layer is the Internet Protocol (IP). IPv4 and IPv6 are versions of the IP protocol.
- Transport Layer-: it is the third layer of four layer of TCP/IP. This layer provides reliable and efficient data transfer between devices, such as error detection, flow control, congestion control, etc. The most common transport layer protocols are Transmission Control Protocol (TCP) and User Datagram Protocol (UDP).
- Application Layer :- it is the top most layer of four layer TCP/IP model. This layer provides services for applications to exchange data, such as email, web browsing, file transfer, etc. A wide range of protocols and services operate at this layer, including HTTP (for web browsing), SMTP (for email), FTP (for file transfer), and many others.
Q 4) Explain the following terms
- File Transfer Protocol (FTP): FTP is used for transferring files between computers on a network. It provides a way to upload and download files to and from servers. For Example: When we upload a document to a cloud storage service or download a software update, FTP or its secure version, SFTP, may be used behind the scenes.
- TELNET(Terminal Network): TELNET is a standard TCP/IP protocol used for virtual terminal service given by ISO. This enables one local machine to connect with another. The computer which is being connected is called a remote computer and which is connecting is called the local computer. TELNET operation lets us display anything being performed on the remote computer in the local computer. This operates on the client/server principle. The local computer uses the telnet client program whereas the remote computer uses the telnet server program.
- HTTP (Hyper Text Transfer Protocol): This protocol is used to transfer hypertexts over the internet and it is defined by the www (world wide web) for information transfer. HTTP is used for transmitting web pages and other resources on the World Wide Web. It defines how web browsers and servers communicate.
For Example: When we enter a web address (e.g., ” https://www.hpbose.org/”) in our browser, HTTP is used to retrieve and display the web page content.
- HTTPS (HyperText Transfer Protocol Secure) :- HTTPS is an extension of the Hypertext Transfer Protocol (HTTP). It is used for secure communication over a computer network with the SSL/TLS protocol for encryption and authentication. So, generally, a website has an HTTP protocol but if the website is such that it receives some sensitive information such as credit card details, debit card details, OTP, etc then it requires an SSL certificate installed to make the website more secure. So, before entering any sensitive information on a website, we should check if the link is HTTPS or not. If it is not HTTPS then it may not be secure enough to enter sensitive information.
- Simple Mail Transfer Protocol (SMTP): SMTP is used for sending email messages. It defines how email clients send messages to email servers and how servers relay messages to their destinations.
For Example: When we send an email from our email client (e.g., Gmail or Outlook), SMTP handles the process of sending the email to the recipient’s email server.
- PPP(Point-to-Point Protocol): It is a communication protocol that is used to create a direct connection between two communicating devices. This protocol defines the rules using which two devices will authenticate with each other and exchange information with each other. For example, A user connects his PC to the server of an Internet Service Provider and also uses PPP. Similarly, for connecting two routers for direct communication it uses PPP.
Q 5) Differentiate between Client Side and Server Side Scripting?
| Client Side Scripting | Server Side Scripting |
|---|---|
| Client Side Scripting used when the users browser already have all the code. | Server Side Scripting used to create dynamic pages. |
| The web browser executes the client side scripting. | The web server executes the server side scripting. |
| The main purpose of this scripting is to give the requested output to the end-user. | The main function of this scripting is to manipulate and grant access to the requested database. |
| More user interaction, as the client side script runs on the client's computer. | Limited user interaction, as the server side script runs on the server. |
| Less secure as client-side code can be easily viewed and altered. | More secure as server-side code is not accessible to the client. |
| Code is accessible to everyone as it is executed on the client's browser. | Code is not accessible to everyone as it is executed on the server. |
| Response from a client-side script is faster as compared to a server-side script. | Response from a server-side script is slower as compared to a client-side script. |
| Languages like JavaScript, CSS, HTML, etc., are very common in the case of client-side scripting. | Languages like PHP, .ASP, Python, etc., come into play in the case of server-side scripting. |
Q 6) What is Javascript? Write the advantages and disadvantages of JavaScript?
JavaScript is a client-side scripting language developed by Brendan Eich. JavaScript can be run on any operating systems and almost all web browsers. We need a text editor to write JavaScript code and a browser to display our web pages.
JavaScript is an object oriented language that allows creation of dynamic and interactive web pages. JavaScript is a high-level, versatile, and widely-used programming language primarily used for web development. It allows developers to add interactive elements and dynamic behavior to websites, making it an integral part of modern web development.
JavaScript advantages
- Fast speed: JavaScript is executed on the client side that’s why it is very fast.
- Easy to learn: JavaScript is relatively simple to learn and implement.
- Browser Compatible: JavaScript supports all modern browsers. It can execute on any browser and produce same result.
- Server Load: JavaScript reduce the server load as it executes on the client side.
- Popularity: JavaScript is a very popular web language because it is used every where on the web.
- Community and Support: JavaScript has a massive and active developer community, which means there is a wealth of resources, tutorials, and third-party plugins available.
JavaScript disadvantages
- Code Visibility: JavaScript code is visible to every one and this is the biggest disadvantage of JavaScript.
- Browser Dependencies: JavaScript behaviour can vary slightly across different browsers, leading to cross-browser compatibility challenges that developers need to address.
- Security Concern: Due to Client-side scripting languages, it is less secure.
Q 7) What is variable ? Write a javascript program to illustrate the variables?
A variable is a container or a symbolic name used to store data or values. Variables allow us to store and manipulate information in our code. They have a name (identifier) and a value associated with them, which can be changed as the program runs.
JavaScript uses reserved keyword var to declare variables. A Variable must have a unique name.
There are some rules while declaring a JavaScript variable.
- Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
- After first letter we can use digits (0 to 9), for example value1.
- JavaScript variables are case sensitive, for example ‘age’ and ‘AGE’ are different variables.
Syntax for declaring JavaScript Variable:-
var <variable-name>=<value>
Example of JavaScript variable-:
<html>
<body>
<script>
var a = “Hello Javascript”;
var b = 20;
var sum=a+b;
document.write(a);
</script>
</body>
</html>
Output-:
Hello Javascript
Q 8) What do you mean by Data Types in Javascript ? Explain?
JavaScript has several built-in data types, which can be categorized into two main groups: primitive data types and Non-primitive (reference) data type.
1) Primitive Data Types: The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
- String: JavaScript Strings are similar to sentences. They are made up of a list of characters, which is essentially just an “array of characters, like “Hello World” etc.
- Number: Represents both integers and floating-point numbers. For example, 3, 3.14, and -42 are numbers.
- Boolean: Represent a logical entity and can have two values: true or false.
- Null: This type has only one value that is null.
- Undefined: A variable that has not been assigned a value is undefined.
2) Non-Primitive Data Types-:The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.
- Object: Represents a collection of key-value pairs, where the keys are strings (or Symbols) and the values can be of any data type.
- Array: array is an object that represents a collection of similar type of elements.
- RegExp: Represents regular expressions, used for pattern matching and text manipulation.
Q 9) What is conditional statements in javascript.explain?
Conditional statements are used to execute different code blocks based on different conditions. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.
In JavaScript we have the following conditional statements:
- if Statement
- if-else Statement
- if-else-if Statement
- switch Statement
1) if Statement: The ‘if’ statement is used to execute a block of code only if a specified condition is true.If the condition is true, the code inside the if block is executed; otherwise, it is skipped.
2) if-else Statement: The ‘if-else’ statement extends the if statement by providing an alternative block of code to execute if the condition is false. If the condition in the ‘if’ part is true, the code in the if block is executed. Otherwise, the code in the ‘else’ block is executed.
3) if-else-if Statement: This statement is used when you have multiple conditions to check in sequence. It allows us to handle different cases with different code blocks.
The conditions are checked one by one, and as soon as one of them evaluates to true, the corresponding code block is executed.
4)Switch Statement: The switch statement is used to select one of many code blocks to be executed.
It’s particularly useful when we want to compare a single value against multiple possible values.
Q 10) What is the difference between if-else and switch statements in javascript?
| if…else statement | Switch statement |
|---|---|
| Depending on the condition in the 'if' statement, 'if' and 'else' blocks are executed. | The user will decide which statement is to be executed. |
| It contains either logical or equality expression. | it contains a single expression which can be either a character or integer variable. |
| This statement is used to choose between two options. | This statement is used to choose among multiple options. |
| It evaluates all types of data, such as integer, floating-point, character or Boolean. | It evaluates either an integer, or character. |
| First, the condition is checked. If the condition is true then 'if' block is executed otherwise 'else' block. | It executes one case after another till the break keyword is not found, or the default statement is executed. |
| If there are multiple choices implemented through 'if-else', then the speed of the execution will be slow. | If we have multiple choices then the switch statement is the best option as the speed of the execution will be much higher than 'if-else'. |
| It is tough to edit if-else statements. | It is easy to modify the switch case. |
Q 11) Explain ‘for’ loop in javascript with an example?
The for loop is used when we know in advance how many times the script should run.
‘for’ loop is a type of loop in JavaScript that is used to repeat a block of code. The syntax of the for loop is as follows:
for (initialization; condition; iteration) {
// code to be executed in each iteration
}
Example-: Program in JavaScript to print name 5 times using for loop.
<html>
<body>
<script type ="text/javascript">
for (i=1; i<=5; i++){
document.write ("My name is Vishal"+ "<br>");
}
</script>
</body>
</html>
Output:
My name is Vishal
My name is Vishal
My name is Vishal
My name is Vishal
My name is Vishal
Q 12)What is the difference between While loop and Do while loop?
1) While Loop:
- In a while loop, the condition is checked before the loop body is executed.
- If the condition is initially false, the loop body will not execute at all.
- If the condition becomes false during the loop execution, the loop will terminate, and the loop body will not execute for subsequent iterations.
Here’s the syntax of a while loop:
while (condition) {
// // code to be executed as long as the condition is true
}
2) Do-While Loop:
- In a do-while loop, the loop body is executed at least once, regardless of whether the condition is true or false.
- After the loop body executes, the condition is checked.
- If the condition is true, the loop continues to execute; otherwise, it terminates.
Here’s the syntax of a do-while loop:
do {
// code to be executed at least once
} while (condition);
Q 13) What do you mean by Javascript Validation? Explain.
Form validation normally used to occur at the server, after the client had entered all the necessary data and then pressed the Submit button. If the data entered by a client was incorrect or was simply missing, the server would have to send all the data back to the client and request that the form be resubmitted with correct information. This was really a lengthy process which used to put a lot of burden on the server.
JavaScript provides a way to validate form’s data on the client’s computer before sending it to the web server.
Creating HTML form using Javascript validation-:
<html>
<head>
<title>Form Validation Example</title>
<script>
function validateForm() {
var name = document.forms["myForm"]["myName"].value;
var email = document.forms["myForm"]["myEmail"].value;
if (name == "") {
alert("Name must be filled out");
return false;
}
if (email == "") {
alert("Email must be filled out");
return false;
}
}
</script>
</head>
<body>
<h2>HTML Form Validation Example</h2>
<form name="myForm" action="/submit.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="myName"><br><br>
Email: <input type="text" name="myEmail"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Q 14) What do you mean by cookies in javascript.Explain?
Cookies in JavaScript are small pieces of data that a web server sends to a user’s web browser and are stored on the user’s device. Cookies are used to store information on the client-side (the user’s browser) that can be sent back to the server with each HTTP request, providing a way to maintain state and store user-specific data across multiple page views or sessions. The next time the user visits the website, the browser sends the cookies back to the server, allowing it to retrieve the stored information.
With JavaScript, a cookie can be created like this:
document.cookie = “username= Vishal Uttam”;
we can also add an expiry date (in UTC time). By default, the cookie is deleted when the browser is closed:
document.cookie = “username=Vishal Uttam; expires=Thu, 18 Dec 2013 12:00:00 UTC”;
With JavaScript, cookies can be read like this:
var x = document.cookie;
document.cookie will return all cookies in one string much like: cookie1=value; cookie2=value; cookie3=value;