This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
toString() doesn't work on numeric literal like 5.toString(), but it works on string literal ("str".toString()) or after the numeric literal is assigned to var, like var a = 5;a.toString(). Wondering why it doesn't work for the first case?
You can do this if you wrap it in parenthesis.
(5).toString();
The first dot is the decimal mark. You need a second one to access the property:
5..toString();
Related
This question already has answers here:
Add method to string class
(6 answers)
how can i repeat a string multiple times according to its index in a string
(3 answers)
Closed 10 months ago.
I need to build this function that recieves a string as parameter and returns a string with each single charcater repeated, for example:
'Boolean'.repeatCharacter()
and this should return:
'BBoolleeaann'
I'm stuck in this, because I cant figure how can I pass a string before the function and it works as parameter??
I tried to make a for cycle using split() to separate each single character and after that other cycle using the character position to print the character but it doesnt work
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.
This question already has answers here:
What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?
(5 answers)
Objects and arrays addition
(4 answers)
Closed 3 years ago.
Why is the result of the expression {}+[] 0?
It appears that the + is treated as a unary operator instead of a normal addition operator. The expression then becomes {}0. Why is this valid JavaScript and why is the result 0? Why is the object literal in the expression ({})+[] treated normally?
Note: I tried searching for a similar question in SO but it doesn't look like searching using symbols works.
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 8 years ago.
I have an object which looks like this:
{"response":{"mydata":[{"xxx:id":"8c8b9703-bc87-40d8-b8d7-f71ebff4002a","Description":"Tsameple desc"].....
Now, my question is, How do I access the Id field?
I have tried
$.response.mydata[i].xxx:id
However, this is resulting in an error thanks to the ":" in the key value. Any tips on how I can get the value?
Use the bracket notation :
response.mydata[i]['xxx:id']
This question already has answers here:
string.charAt(x) or string[x]?
(7 answers)
Closed 8 years ago.
What is the difference between str[0] and str.charAt(0)? I always access specific character by simply typing str[i], where i is the index of the character I want to access (counted from 0), but last week I had good overview of open source JS code, and in every single project I saw, people use charAt method.
Is there any difference between those two ways?
[] is a more primitive way of accessing all kind of arrays.
charAt() is specific to strings.
You can access the index of any array by using [], but you can only use charAt() on a string.
But when it comes to string alone, both are one and the same.
But you should use charAt() because, it is well supported in all the major browsers, while the bracket notation will return undefined in IE7
Also, the bracket notation is simply accessed, while charAt() does some validation and doesn't return undefined, but will return an empty string ""
This
""[-4] // undefined
"".charAt(-4) // ""
You could ensure that the result would be a string.