P
P
Preis Dmitry2018-01-17 21:35:46
css
Preis Dmitry, 2018-01-17 21:35:46

Should you use a++ in JavaScript?

Recently I began to study JavaScript, and I had a question: isn't it better to use the prefix form of notation of operators when adding one to a variable?

var a = 1;
a++; //постфикс
++a; //префикс

Why do we need a postfix notation at all and can it be applied somewhere in practice, instead of a prefix one? After all, it's better for us when incrementing is done immediately?
I understand that the question is from the category "dad, why do birds fly?", but still I really want to know what is better and what is better to use

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lagudal, 2019-11-04
@lagudal

keywords "stroke-dasharray, stoke-dashoffset" - see the implementation in google for examples of darkness.
Requirements for svg - fill none, stroke-width to your liking.
And not on click, but on hover, an element with core__card js-core-card classes is added with the core__card_active class,
and this animation is applied to it.
For example, the first element, div data-id="insurance" class="core__card js-core-card
inside it has svg id="insuranceSvg", stroke-dashoffset=32.
Once the core__card_active class is added to the div, the stroke-dashoffset value is animated to 0. The active class is removed, the value of stroke-dashoffset is back to default.Well, there is still a concern for the active class, but this is no longer svg - it's just everything.

S
Stalker_RED, 2018-01-17
@Preisy

Yes, this is a necessary thing, and according to my observations, the postfix one is much more common than the prefix one.
Postfix returns the current value, and then increments the variable. Prefix, increments first, and returns the already incremented.
GL3DHGA.png

spoiler
let i = 0, zzz = 'abcdef'
while(i < zzz.length) {
  console.log(zzz[i++])
}

E
EEclipsEE, 2018-01-17
@EEclipsEE

It's not the same thing. Run console following code.

var a=1, b=1;

console.log(a++);
console.log(++b);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question