What's the meaning and use of "res" in function arguments?

21,106

Solution 1

In case it's not quite clear from the various answers and comments, in this context req is short for request and res is short for response.

Solution 2

A HTTP transaction consists of a request from the client to the server and a response from the server to the client.

Solution 3

In your code, req means request and res response, but res can also mean "result". A JS example:

function count(haystack, needle) {
    var res = 0

    arr.forEach(function (value) {
        if (value === needle) {
            res++
        }
    })

    return res
}

Solution 4

Not an answer, but

It seems to be pretty commonplace to use the word "res" for one of the indexes in function arguments. Its existence appears to be agnostic to whatever programming language you look at.

Nope. My guess is this is only common for Javascript doing http request/response (or a few very similar things).

Share:
21,106
Henrik
Author by

Henrik

Fine artist, CG/3D/VFX-learner, musician.

Updated on July 09, 2022

Comments

  • Henrik
    Henrik almost 2 years

    here's a short question that my googling failed to deliver any clues on.

    It seems to be pretty commonplace to use the word "res" for one of the indexes in function arguments. Its existence appears to be agnostic to whatever programming language you look at.

    What does it stand for? A simple guess would be, "resource", perhaps? If this is spot on, it would be nice if someone who feels pedagogically inclined, would care to help me shed some light on this. What I don't grok, in particular, is when and why there's a use for the meaning, "resource" (granted that this is the actual meaning of "res").

    Edit 1: I'm providing a random example, this is from the NodeJS.org homepage:

    var http = require('http');
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World\n');
    }).listen(8124, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:8124/');

    Edit 2: I first came across the "res" thing while working with a PHP backend developer on a fairly modded WP instance. At the time things were too hectic to really sort out what he meant in his quick explanation, but he seemed to scatter the word all around, in arguments and $res variable calls alike. It's obviously impossible to comment on individual programmers' style and preference of coding, but I'm mostly curious to know if there's some sort of untold consensus among programmers – and if there's a single purpose – using a so called "res" pointer. (Compare eg. with using i for "iterator" as often used in loops.)