Declare a string var with multiple lines in JavaScript/jQuery
You can use \
to indicate that the line has not finished yet.
var h= '<label>Hello World, Welcome to the hotel</label> \
<input type="button" value="Visit Hotel"> \
<input type="button" value="Exit">';
Note: When you use \
, the whitespace in the following line will also be a part of the string, like this
console.log(h);
Output
<label>Hello World, Welcome to the hotel</label> <input type="button" value="Visit Hotel"> <input type="button" value="Exit">
The best method is to use the one suggested by Mr.Alien in the comments section, concatenate the strings, like this
var h = '<label>Hello World, Welcome to the hotel</label>' +
'<input type="button" value="Visit Hotel">' +
'<input type="button" value="Exit">';
console.log(h);
Output
<label>Hello World, Welcome to the hotel</label><input type="button" value="Visit Hotel"><input type="button" value="Exit">
In ES6 you can achieve this by simply declaring a variable as:
const sampleString = `Sample
text
here`;
This in return evaluates to 'Sample \ntext\nhere'
You can read all about ES6 rules of multiline strings from here.
Edit
Now you could also make a use of ES6 Template Literals.
let str = `
some
random
string
`;
You can also interpolate the variables in the above string with an ease, without using concatenation, like:
let somestr = 'hello',
str = `
${somestr}
world
`;
Old answer
@thefourtheye answer is perfect, but if you want, you can also use concatenation here because sometimes \
will be misleading, as you will think those are literal characters ..
var h = '<label>Hello World, Welcome to the hotel</label>';
h += '<input type="button" value="Visit Hotel"> ';
h += '<input type="button" value="Exit">';
console.log(h);
Demo