Push Push : // Creating An Empty Array Let Numbers

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

what are the server-side and client-side scripting?

explain your answer with suitable example


Server-side scripting and client-side scripting are two different approaches to handling the execution
of code in web applications. Here's an explanation of each with suitable examples:

1. Server-side scripting: Server-side scripting refers to executing code on the server before sending the
final result to the client's browser. The server processes the script and generates a response that is
then sent to the client. Common server-side scripting languages include PHP, Python (with
frameworks like Django), Ruby (with frameworks like Ruby on Rails), and Node.js.

Example: Let's consider a basic login system. When a user submits their login credentials, the client
sends a request to the server. The server-side script receives the request, validates the credentials
against a database, and generates a response indicating whether the login was successful or not. The
server-side script handles all the backend logic, database interactions, and authentication processes.

2. Client-side scripting: Client-side scripting refers to executing code directly on the client's browser
using languages such as JavaScript. The code is downloaded along with the web page and executed
by the client's browser. Client-side scripting is primarily used for enhancing the user experience and
interactivity of web pages without requiring frequent server requests.

Example: Consider a form validation scenario. When a user fills out a form and submits it, the client-
side script (usually written in JavaScript) can validate the form's data directly in the browser without
making a round-trip to the server. The script can check if the required fields are filled, validate email
formats, enforce password requirements, and provide real-time feedback to the user.

In summary, server-side scripting involves executing code on the server to generate a response for
the client, while client-side scripting involves executing code directly in the client's browser to
enhance the user experience. Both approaches have their specific use cases and complement each
other to create dynamic and interactive web applications.

what is the use of the push method in javascript? explain your answer with suitable example
In JavaScript, the push() method is used to add one or more elements to the end of an array and
returns the updated length of the array. It modifies the original array by appending the new
element(s) at the end. The push() method is commonly used to dynamically add elements to an
array or to build an array iteratively.

Here's an example to illustrate the usage of the push() method:

// Creating an empty array

let numbers = [];

// Adding elements to the array using push()


numbers.push(1);

numbers.push(2);

numbers.push(3);

console.log(numbers); // Output: [1, 2, 3]

console.log(numbers.length); // Output: 3 In the example above, we start with an empty array called
numbers. Then, we use the push() method to add three elements ( 1, 2 , and 3 ) to the array
sequentially. After each push() operation, the array is updated, and the new element is added to the
end. Finally, we log the resulting array and its length to the console.

The push() method is versatile and allows adding multiple elements at once by passing them as
separate arguments:

let fruits = ['apple', 'banana'];

fruits.push('orange', 'grape');

console.log(fruits); // Output: ['apple', 'banana', 'orange', 'grape']

console.log(fruits.length); // Output: 4 In this example, we have an array fruits initially containing two
elements. We use the push() method to add two more elements ( 'orange' and 'grape') to the end
of the array.

The push() method is an essential tool for managing arrays in JavaScript, allowing dynamic
expansion and modification of array contents by easily appending elements to the end.

write a program to print the name of schools using array and also find the length of this array in
java script, the name of schools are as below: 1. Shining star school, 2. city public school, 3. kids
grammar school

// Array containing the names of schools

let schoolNames = [

"Shining star school",


"City public school",

"Kids grammar school"

];

// Printing each school name

for (let i = 0; i < schoolNames.length; i++) {

console.log(schoolNames[i]);

// Calculating and displaying the length of the array

console.log("Length of the array: " + schoolNames.length);

Q2 b: write a program for adding 3 numbers using javascript function

// Function to add three numbers

function addThreeNumbers(num1, num2, num3) {

return num1 + num2 + num3;

// Test the function

let result = addThreeNumbers(5, 10, 15);

console.log("Sum of three numbers: " + result);

You might also like