I\'d like for something like 5 + 6
to return \"56\"
instead of 11
.
I converted back to number like this..
const timeNow = '' + 12 + 45;
const openTime = parseInt(timeNow, 10);
output 1245
-- edit --
sorry,
for my use this still did not work for me after testing . I had to add the missing zero back in as it was being removed on numbers smaller than 10, my use is for letting code run at certain times May not be correct but it seems to work (so far).
h = new Date().getHours();
m = new Date().getMinutes();
isOpen: boolean;
timeNow = (this.m < 10) ? '' + this.h + 0 + this.m : '' + this.h + this.m;
openTime = parseInt(this.timeNow);
closed() {
(this.openTime >= 1450 && this.openTime <= 1830) ? this.isOpen = true :
this.isOpen = false;
(this.openTime >= 715 && this.openTime <= 915) ? this.isOpen = true :
this.isOpen = false;
}
The vote down was nice thank you :)
I am new to this and come here to learn from you guys an explanation of why would of been nice.
Anyways updated my code to show how i fixed my problem as this post helped me figure it out.
just use:
5 + "" + 6
simple answer:
5 + '' + 6;
var output = 5 + '' + 6;
I'd prefer the concat
way :
const numVar1 = 5;
const numVar2 = 6;
const value = "".concat(numVar1, numVar2);
// or directly with values
const value2 = "".concat(5, 6);
You can also use an array which can help in some use cases :
const value3 = [5, 6, numVar1].join('');
To add to all answers above I want the share the background logic:
Plus is an addition operator that is also used for concatenation of strings. When we want to concatenate numbers. It should be the understanding that we want to concatenate the strings, as the concatenation of numbers doesn't make valid use cases to me.
We can achieve it in multiple ways,
Through type conversion
let a = 5;
a.toString()+5 // Output 55 type "string"
This will also work and doing type conversion in the background,
5 +""+ 5 // Output 55 type "string"
If you are determined to concatenate two string and type of output should be int, parseInt() works here
parseInt(5 +""+ 5) //Output 55 Type "number"