What does ++ mean in jQuery/JavaScript? [duplicate] - javascript

This question already has an answer here:
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
I've seen various people use stuff like i++, and I know that is also used in a for-loop.
But what exactly does ++ do to a variable? I cannot seem to find any documentation on what it does.

The ++ notation is the increment operators.
i++
is the same thing of
i = i +1
here you can see the completly list of this operators:
http://www.w3schools.com/js/js_operators.asp

It's to increment.
var i = 1;
i++; // i becomes 2.

Related

Why in JS a.test = 1 when "a" is a number doesn't throw an error [duplicate]

This question already has answers here:
How is almost everything in Javascript an object?
(6 answers)
Closed 5 months ago.
I tested this today and I don't understand the behaviour. For me, that should be an error.
const a = 1;
a.test = 1;
console.log(a)
console.log(a.test)
Someone have the answer ?
you can define properties pretty much anything, but in this case you are assigning it to a number which doesn't actually hold properties.

increase object value on javascript? [duplicate]

This question already has answers here:
What's the difference between ++i and i++ in JavaScript [duplicate]
(6 answers)
Closed 1 year ago.
I want to increase object value just like loop i++ , but in object didnt worked ,
how to increase this value ? any methods?
const object = {
price:30
}
console.log(object.price++);
using ++ like that is the post-increment operator meaning object.price++ returns object.price first then increments. You want the pre-increment operator ++object.price.
const object = {
price:30
}
console.log(++object.price);

Can I generate new array with another variable in javascript? [duplicate]

This question already has answers here:
What do square brackets around an expression mean, e.g. `var x = a + [b]`?
(7 answers)
Closed 7 years ago.
function construct(head, tail) {
return cat([head], _.toArray(tail));
}
I'd like to know the role of '[' and ']' above.
Is it an operator?
Is it the array initialize literal?
This question is answered here and on the previous QnA(Use of [square brackets] around JavaScript variables).
I felt this weird because I am not used to javascript.
But this is not a matter any more.
I thought wrong and this is explained. Thanks!
It is initializing a 1 element array to pass to cat here. Brackets can be used to access elements on objects and arrays, and to represent an array literal like you have here.

What does '=>' imply in typescript/javascript? [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
So I have saw tons of '=>' in some code I found online. Can anyone explain to me like I'm 5?
(I am looking for code, and I will post it here once I find it)..
Got it:
var directive = () =>
{
return new MyDirective();
};
this is ECMASCRIPT 6 standard arrow function.
In this case directive is assigned a function with 0 arguments and one return statement
Details are documented here in MDN Docs
()=> is simply a lambda function, which (in this case) means nothing more than a shorthand notation for a function without a name taking 0 paramaters.
You could have written var directive=function(){return new MyDirective();};
Take a look at John Papas blog Post.

JavaScript parentheses (1,2,3,4,5) [duplicate]

This question already has answers here:
Javascript expression in parentheses
(2 answers)
Closed 8 years ago.
I just came across some notation in JavaScript like so:
var a = (1,2,3,4,5);
This will always return the last value, in the above case 5. I'm aware of using brackets to namespace my JavaScript code, but have never seen it used this way.
Is there any use for this notation, or is it just some JavaScript byproduct?
It's the comma operator. As the mdn states (link) it always returns the later value. In your example it doesn't make much sense, since it will always assign a = 5. But consider this:
for (var i = 0, j = 9; i <= 9; i++, j--) {
...
}
It's used to increment and decrement in a single statement: i++, j--
Edit:
The parentheses in your example are necessary because its a variable declaration. In other cases they can be left out.
Parens are used to groups operations together. This is helpful for both setting operation precedence (e.g. x = (2+3) * 5 vs x = 2 + 3 * 5) and for making your code a little easier to read.
I suspect this is more a question about the comma operator. This is for making multiple assignments or operations on the same line. Here is a nice article about it: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

Categories

Resources