0

Why this code do not work ?

var text = '';
var text += 'How ';
var text += 'are ';
var text += 'you ';
var text += 'today ?';

alert(text);

I'm trying to alert() : How are you today ?

This is a voluntary basic code.

Thanks.

2
  • 3
    take var out after the first one =] Commented Jan 27, 2017 at 14:23
  • 1
    you only need to define text once, kill the var in front of text after the first one Commented Jan 27, 2017 at 14:23

8 Answers 8

3

Because you are re-declaring text variable each time instead of just updating its value. You should do:

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';

alert(text);
Sign up to request clarification or add additional context in comments.

1 Comment

comprehensively explained
2

You are redeclaring the variable on each line.

You should use var to declare the variable first.

After, you could add some other text by using the += symbol.

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';
alert(text);

Comments

1

You are redeclaring the variable each time. Drop the extra vars, you only need the first one.

Comments

1

Simply remove the var when you append new value.

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';

alert(text);

You just use var when you want to declare a JavaScript variable

Comments

1

Each time you use var text, you are re-declaring the variable.

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';

alert(text);

Here's a different approach, using an array:

var text = [];

text.push('How');
text.push('are');
text.push('you');
text.push('today');

alert( text.join(' ') );

Comments

1

I'll answer in a bit more depth.


The var keyword could be considered a constructor. What a constructor does is creates something, of a class, with all default values. By default, values constructed with the var keyword are null.

With that in mind, what you are doing in reality is redeclaring the text variable as null, and then assigning it your next value, but you are doing this each time you want to add something, therefore appending your new value onto nothing, meaning it is assigned to that value, and that value alone.

This is your solution: (don't redeclare)

var text = "Hello",
    name = "Josh";

text += " World";
text += ", My name is ";
text += name;

Comments

0

You cannot use += while declaring a variable in JavaScript.

Also, you are making multiple declarations of the same variable.

Try this instead -

https://jsfiddle.net/abto5aLj/

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';
alert(text);

Comments

0

Because you are redefining the variable text every time our are trying to add a new piece of string.

You have to use the variable statement var only the first time you are defining a new variable (text in your case).

Here the complete example with the correction:

var text = '';
text += 'How ';
text += 'are ';
text += 'you ';
text += 'today ?';

alert(text);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.