The snippet url += i > 0 is not performing a comparison between the url (a string) and 0 (a number), rather it does a comparison between i (the loop counter) and the constant 0; and both are numbers.
In order to understand what is happening, you first have to understand two things:
- the ternary operator
?:
- the shortcut assignment operator
+=
I will address the shortcut assignment operator first. Then I will explain the ternary operator.
The shortcut assignment operator: +=
The shortcut assignment operator lets you increment and assign a value in one step. Rather than doing something like this:
a = a + 5; // first increment a by 5, then assign the result to a
This operator lets you do something like this:
a += 5; // increment and assign the result of increment a by 5 in one step
The ternary operator: ?:
The ternary operator is a shortcut way to perform an if...else operation. It is called a ternary operator because it accepts three (3) operands. For example, the classical if...else statement is written like this:
if(condition) {
// code to execute if condition is true
} else {
// code to execute otherwise
}
The ternary operator lets you do the above in a more elegant and concise way:
condition ? code_to_execute_if_condition_is_true : code_to_execute_otherwise
In the above snippet, the operator is the ?:, and the operands are as follows:
condition
code_to_execute_if_condition_is_true
code_to_execute_otherwise
The operation can also be broken into two parts:
The first part is the part from the condition up until the :, and is similar to the if(condition) of a classical if...else. If that condition is true, the code between the ? and the : will be executed. It is just as if you wrote:
if(condition) {
// code_to_execute_if_condition_is_true
}
The second part is the part following the :, and the code following it is executed if the condition is false. It is as if you wrote:
else {
// code_to_execute_otherwise
}
Important: But please note that the ternary operator is one: ?: you cannot use either part in isolation. They must always be used as specified above.
Addressing the issue at hand
Now, coming to the issue at hand, this is your code:
let url = "/user_auth/devices";
const params = ["clientId = 909090", "name = peter", "active = true"];
for (let i=0; i < params.length; i++) {
url += i > 0 ? "&" + params[i] : "?" + params[i];
}
I will first explain the part inside the loop:
url += i > 0 ? "&" + params[i] : "?" + params[i];
This code snippet can be split into two parts:
url +=
Based on what we have explained so far, this is a shortcut way of writing:
url = url + // something (we will get to that something in a bit)
i > 0 ? "&" + params[i] : "?" + params[i];
This is the ternary operator at play here, and it is like writing:
if(i > 0) {
return "&" + params[i];
} else {
return "?" + params[i];
}
Earlier, we talked about url += as being equivalent to url = url + // something. That something is what is returned from the second part:
i > 0 ? "&" + params[i] : "?" + params[i];.
And whatever that is is tacked onto the end of the url.
So that if i is greater than 0 (which is the case in all but the first iteration), the url will be
url = url + "&" + params[i].
However, if i is 0 (which is the case on the first iteration), the url will be
url = url + "?" + params[i].
Since your initial url string is: "/user_auth/devices";, substituting the different iteration values for params[i], we will have:
on the first iteration,
i === 0
params[i] equals "clidenId = 909090"
So, we will have url = url + "?" + "clientId = 909090".
Resulting in: "/user_auth/devices?clientId = 909090";
On the second iteration,
i === 1 (and therefore greater than 0)
params[i] equals "name = peter"
we will have url = url + "&" + "name = peter"
Resulting in: "/user_auth/devices?clientId = 909090&name = peter";
The same process is repeated on the third iteration (and any subsequent iteration if you add some other elements to the params array). But since i will be greater than 0 in all subsequent iterations, the result will be obtained via a similar process to the second iteration.
url += (i > 0 ? ("&" + params[i]) : ("?" + params[i]));