Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is += in Javascript? [duplicate]

For example, in the while loop:

while (i < 10) {
    text += "The number is " + i;
    i++;
}

What does it do? Thanks.

like image 689
HappyHands31 Avatar asked Oct 05 '15 20:10

HappyHands31


People also ask

Are there duplicates JavaScript?

In JavaScript, an object consists of key-value pairs where keys are similar to indexes in an array and are unique. If one tries to add a duplicate key with a different value, then the previous value for that key is overwritten by the new value.

What is a duplicate element?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

What is duplicated value?

A duplicate value is one in which all values in at least one row are identical to all of the values in another row. A comparison of duplicate values depends on the what appears in the cell—not the underlying value stored in the cell.


1 Answers

It is the addition assignment operator (+=) to add a value to a variable.

Depending of the current type of the defined value on a variable, it will read the current value add/concat another value into it and define on the same variable.

For a string, you concat the current value with another value

let name = "User";

name += "Name"; // name = "UserName";
name += " is ok"; // name = "UserName is ok";

It is the same:

var name = "User";

name = name + "Name"; // name = "UserName";
name = name + " is ok"; // name = "UserName is ok";

For numbers, it will sum the value:

let n = 3;

n += 2; // n = 5
n += 3; // n = 8

In Javascript, we also have the following expressions:

  • -= - Subtraction assignment;

  • /= - Division assignment;

  • *= - Multiplication assignment;

  • %= - Modulus (Division Remainder) assignment.

like image 169
Felipe Oriani Avatar answered Oct 14 '22 23:10

Felipe Oriani