var myCoolString = myCoolObject.a + '-' + myCoolObject.b + '-' + myCoolObject.c;
EDIT:
With ES6, you can use template strings to interpolate numbers into strings:
let myCoolString = `${myCoolObject.a}-${myCoolObject.b}-${myCoolObject.c}`;
Try it:
var myCoolObject = {
a: 0,
b: 12,
c: 24
};
var myCoolString = myCoolObject.a + '-' + myCoolObject.b + '-' + myCoolObject.c;
console.log(typeof myCoolString);
console.log(myCoolString);
Answer from rink.attendant.6 on Stack Overflowvar myCoolString = myCoolObject.a + '-' + myCoolObject.b + '-' + myCoolObject.c;
EDIT:
With ES6, you can use template strings to interpolate numbers into strings:
let myCoolString = `${myCoolObject.a}-${myCoolObject.b}-${myCoolObject.c}`;
Try it:
var myCoolObject = {
a: 0,
b: 12,
c: 24
};
var myCoolString = myCoolObject.a + '-' + myCoolObject.b + '-' + myCoolObject.c;
console.log(typeof myCoolString);
console.log(myCoolString);
how can I do this in JS without [...] writing too much code?
If you know all of your numbers are positive, you can write even less code to achieve the same as we know JavaScript executes left-to-right.
var obj = {a: 0, b: 12, c: 24}; /* define object
String + Number = String
String + Number = String
String + Number = String
String + Number + Number + Number = String */
'' + obj.a + -obj.b + -obj.c; // = "0-12-24"
The - were inserted because String + -Number = "String-Number", e.g.
'AK' + -47 // "AK-47"
Videos
Your code is correct. Perhaps your problem is that you are not passing an ID to the AddBorder function, or that an element with that ID does not exist. Or you might be running your function before the element in question is accessible through the browser's DOM.
Since ECMAScript 2015, you can also use template literals (aka template strings):
document.getElementById(`horseThumb_${id}`).className = "hand positionLeft";
To identify the first case or determine the cause of the second case, add these as the first lines inside the function:
alert('ID number: ' + id);
alert('Return value of gEBI: ' + document.getElementById('horseThumb_' + id));
That will open pop-up windows each time the function is called, with the value of id and the return value of document.getElementById. If you get undefined for the ID number pop-up, you are not passing an argument to the function. If the ID does not exist, you would get your (incorrect?) ID number in the first pop-up but get null in the second.
The third case would happen if your web page looks like this, trying to run AddBorder while the page is still loading:
<head>
<title>My Web Page</title>
<script>
function AddBorder(id) {
...
}
AddBorder(42); // Won't work; the page hasn't completely loaded yet!
</script>
</head>
To fix this, put all the code that uses AddBorder inside an onload event handler:
// Can only have one of these per page
window.onload = function() {
...
AddBorder(42);
...
}
// Or can have any number of these on a page
function doWhatever() {
...
AddBorder(42);
...
}
if(window.addEventListener) window.addEventListener('load', doWhatever, false);
else window.attachEvent('onload', doWhatever);
In javascript the "+" operator is used to add numbers or to concatenate strings. if one of the operands is a string "+" concatenates, and if it is only numbers it adds them.
example:
1+2+3 == 6
"1"+2+3 == "123"
From my understanding of what I've read, the expected behavior is that JavaScript will convert the integer into string, and then add the string to string, which then results in a string concatenation. Therefore...
var addit = 6 + “5”;
...should output... "65";
However, attempting to evulate this in console instead gives the result...
Uncaught SyntaxError: Invalid or unexpected token
Why is this the case?
Use "" + 5 + 6 to force it to strings. This works with numerical variables too:
var a = 5;
var b = 6;
console.log("" + a + b);
You can now make use of ES6 template literals.
const numbersAsString = `${5}${6}`;
console.log(numbersAsString); // Outputs 56
Or, if you have variables:
const someNumber = 5;
const someOtherNumber = 6;
const numbersAsString = `${someNumber}${someOtherNumber}`;
console.log(numbersAsString); // Outputs 56
Personally I find the new syntax much clearer, albeit slightly more verbose.