How to add multiple values in single variable using javascript?

How can I get multiple values in a single variable like this:

1, 2, 3, 4, 5

$('.icon').click(function() {
  var totalWishlist;
  $(this).toggleClass('active');
  totalWishlist += $(this).attr('data-pid');
  console.log(totalWishlist);          
});
.active{color:red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="icon" data-pid="1">icon 1</div>
<div class="icon" data-pid="2">icon 2</div>
<div class="icon" data-pid="3">icon 3</div>
<div class="icon" data-pid="4">icon 4</div>

You're getting undefined1 etc. because you don't declare totalWishlist with a value, so by default it's undefined. To fix that, define it as an empty string outside of the click handler:

var totalWishlist = '';

However, it would make far more sense to use an array and push() to it. This is far easier to modify. If you want to retrieve a string from that array you can use join(', '), like this:

var totalWishlist = [];

$('.icon').click(function() {
  totalWishlist.push($(this).data('pid'));
  console.log(totalWishlist.join(', '));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="icon" data-pid="1">icon 1</div>
<div class="icon" data-pid="2">icon 2</div>
<div class="icon" data-pid="3">icon 3</div>
<div class="icon" data-pid="4">icon 4</div>

Using Javascript's es6 or node, you can do the following: var [a,b,c,d] = [0,1,2,3] And if you want to easily print multiple variables in a single line, just do this: console.log(a, b, c, d) 0 1 2 3. This is similar to @alex gray 's answer here, but this example is in Javascript instead of CoffeeScript.

Use an array and move it outside of your function. If you want to add the items only once, use includes to check if the item is already in the array and skip it:

const totalWishlist = [];

$('.icon').click(function() {
  const pid = $(this).attr('data-pid');
  if (!totalWishlist.includes(pid)) {
    totalWishlist.push(pid);
  }
  console.log(...totalWishlist);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="icon" data-pid="1">icon 1</div>
<div class="icon" data-pid="2">icon 2</div>
<div class="icon" data-pid="3">icon 3</div>
<div class="icon" data-pid="4">icon 4</div>

I want to set the following variables to the same value in one single line Example: export A=B=C=20 There is a syntax available in 'bash' but how can I accomplish the above in ksh ?Ksh93 doesn't have such expressions, so it's better to make it explic

Make the variable an string And define it outside the function. Make it global.

var totalWishlist="";
$('.icon').click(function() {
    
    
      totalWishlist+=$(this).attr('data-pid')+ " ";
      console.log(totalWishlist);
      
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="icon" data-pid="1">icon 1</div>
<div class="icon" data-pid="2">icon 2</div>
<div class="icon" data-pid="3">icon 3</div>
<div class="icon" data-pid="4">icon 4</div>

A variable, as the name says is a name given to a memory location where different values can be stored and is associated with a symbolic name. Javascript is no exception and variables are expressed by ‘var’ keyword followed by its symbolic name.

Once they select one of the choices, I want to redirect them to a different page that shows them all ten of their selections. To do this I created a variable for all ten decisions, and want to display that variable on the other page. Here is my code for the first page (draft_database.js) (to simplify, I only showed one of the ten decisions):

In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input. A variable declared without a value will have the value undefined. The variable carName will have the value undefined after the execution of this statement:

An array can hold many values under a single name, and you can access the values by referring to an index number. Creating an Array Using an array literal is the easiest way to create a JavaScript Array.

Comments
  • Arrays are intended for exactly this purpose
  • You forgot to initialize totalWishlist to anything, so +=ing anything to it won't work (you should also coerce the attribute to a number)
  • Not only so, OP is overwriting totalWishlist in the click handler.
  • Also, var totalWishlist should be outside $('.icon').click(function() {})
  • It is declared, but not initialized. It exists, but is still undefined. It is different from var totalWishlist = "";
  • i don't need array, we cant do it without array?
  • Using an array makes the solution to your problem much easier to work with and far less hacky. If you want to get a string back out of the array just use join(',')
  • @RohitVerma It does not matter. What you want is to store a "collection" of the pid, and array is the best way to do it. If you want to create the string 1, 2, 3, 4 later, you can simply join the array, e.g.. totalWishList.join(', ')
  • @RohitVerma You don't know how to store multiple values in a single variable, but you know you don't need arrays? Interesting
  • @JeremyThille indeed. This is a very odd question as the OP has historically asked (and even answered) questions on much more complex topics than basic array use. This seems almost like a shared account, or one which has been illegitimately accessed.
  • I think OP wants to concatenate the values, not sum them up.
  • multiple values like this 1, 2, 3, 4, 5, doesn't seem to me like OP wants to concatenate them