JavaScript: Client side Vs. Server side

Client side programming includes any coding or computation or effects or animation or any sort of interaction your website performs with the user via browser . But server side programming is that which performs all the task in the server only . So the user is unaware of that. Few years ago JavaScript compilers were available only on the client machine (browsers). So java script was called as a client side scripting language. On the client side JavaScript is run by v8 engine (Google chrome). But now in the server side also JavaScript is used. The v8 engine (with some modifications to provide the server functionality) is also used in the servers to run js codes. So, in both cases the language is the same, only the environment is different.

Client-side JavaScript

Client-side JavaScript refers to JavaScript code that is executed on the user's web browser. It's used to enhance the functionality and interactivity of web pages without requiring communication with the server. Common uses of client-side JavaScript include form validation, dynamic content updates, and user interface enhancements.

<!DOCTYPE html> <html> <head> <title>Client-side JavaScript Example</title> <script> function greet() { var name = prompt("Enter your name:"); alert("Hello, " + name + "!"); } </script> </head> <body> <button onclick="greet()">Click me</button> </body> </html>

In this example, when the "Click me" button is clicked, the client-side JavaScript code prompts the user for their name and displays a greeting using an alert box, all within the user's browser.

Server-side JavaScript

Server-side JavaScript refers to JavaScript code that is executed on the server, usually in response to HTTP requests. It's used to generate dynamic content, handle data processing, and interact with databases. Server-side JavaScript is often used in conjunction with server-side frameworks and technologies like Node.js to build web applications and APIs.

Example (Node.js):
const http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello from the server!'); }).listen(8080);

In this Node.js example, server-side JavaScript code creates a simple HTTP server that responds with "Hello from the server!" when a request is made to the specified port.

Client side Vs. Server side

Location of Execution

  1. Client-side JavaScript runs in the user's web browser.
  2. Server-side JavaScript runs on the web server.

Purpose

  1. Client-side JavaScript enhances user interactivity and experience within the browser.
  2. Server-side JavaScript is used to generate dynamic content, process data, and handle server-side tasks.

Access to Resources

  1. Client-side JavaScript has access to the browser's Document Object Model (DOM) and can manipulate webpage content.
  2. Server-side JavaScript can access databases, file systems, and other server resources.

Communication

  1. Client-side JavaScript communicates with the server via AJAX requests or by loading new pages.
  2. Server-side JavaScript processes client requests and sends responses back to the client.

Conclusion

Client-side JavaScript focuses on enhancing the user experience within the browser, while server-side JavaScript is used to manage server-related tasks and generate dynamic content on the server before sending it to the client.