jakub
jakub

Reputation: 5114

JavaScript: Which machine does the computation?

I am not sure how to properly phrase this (probably embarrassingly basic) question, but here's the point:

When I perform an operation using JavaScript (say, loop over a set of values and calculate square root of each value), which machine is making the calculation? Is it my PC, or the server on which the script is placed?

(The script is part of a webpage that allows users to upload some data on which then calculations are performed using JavaScript, and the question is, would the actual computation be performed by their PCs or the server that hosts that page?)

Upvotes: 1

Views: 126

Answers (4)

Quentin
Quentin

Reputation: 943580

It depends.

JavaScript embedded in a webpage using a <script> element (with a few exceptions that would be quite obvious if you were dealing with them) will be executed by the browser on the client machine.

JS is just a programming language though, so you can run it on the server. Node.js is probably the most popular way (of many) to do that these days. You can also run it without any web involvement (e.g. with Windows Scripting Host for Windows programming or Espruino Pico for embedded device programming).

Upvotes: 2

Brenton Alker
Brenton Alker

Reputation: 9072

JavaScript is a originally/commonly a client-side language. The code is sent to the client (the users browser) and it is executed there. The server doesn't need to do anything except serve static files.

There are (more popular recently) instances of server-side JavaScript; such as node.js; but this is a different model entirely, where the server runs JavaScript code to generate content (usually HTML or JSON) to be sent to the client.

The two are often used in combination, with the server-side component generating data and sending it to a client-side component for display and manipulation by the user.

Upvotes: 1

jered
jered

Reputation: 11581

JavaScript is a typically used as a client-side scripting language, it always runs on the client machine. When someone views a webpage, they download all the necessary files onto their local computer, including HTML and JavaScript, and it runs there. This is in contrast to something like PHP or Node.js where the server runs some kind of calculation or script BEFORE sending data to the client.

Upvotes: 1

Andreas Kruhlmann
Andreas Kruhlmann

Reputation: 161

JavaScript is a client side script, which means it runs "in your browser." For the same reason you can actually run JavaScript by typing

javascript:your code here

into the URL bar.

Upvotes: -2

Related Questions