What happens when we declare Array(4) in JavaScript? [duplicate] - javascript

This question already has answers here:
Javascript new Array and join() method
(5 answers)
Closed 6 years ago.
console.log(Array(4).join("hi"));
>> "hihihi"
I don't get what exactly is happening here?

join() is the opposite of split(). whereas split separates an array by the delimiting character you pass it, join will instead combine all the elements delimiting each one with whatever parameter you pass.
In this case the array is simply Array(4), so 4 undefined elements. combining these will yield "undefinedhiundefinedhiundefinedhiundefined".
Since js doesn't actually treat undefined as anything in this case, it turns it into an empty string and all you get is hihihi
edit: reference for my last statement from the join() documentation:
The string conversions of all array elements are joined into one string. If an element is undefined or null, it is converted to the empty string.

Related

How to write a function in the global object prototype String [duplicate]

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

Convert array of strings to array of numbers [duplicate]

This question already has answers here:
convert string array to integer array
(4 answers)
Closed 4 years ago.
I have a jQuery array ["3434", "3433"]
And I would like to make it like [3434, 3433]
No need for jQuery. Supposing you're sure they're all numbers (that is, we're not checking for errors), you can map them with Number, which converts them from string to numbers.
["3434", "3433"].map(Number);
Considering that Number returns NaN in case of error, you may then want to filter the result to remove undesired elements.
let nums = ["3434", "3433", "foo"].map(Number).filter(n => !isNaN(n));
console.log(nums);

Cannot call toString() on numeric constant in Javascript [duplicate]

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();

Javascript two dimensional string array update value [duplicate]

This question already has answers here:
Why can't I swap characters in a javascript string?
(6 answers)
Closed 7 years ago.
I have the following code:
var data = ["z wwwww ","www w ","w b w ww","w w p w","w w w","wwbwp w"," wy www"," wwwww "];
console.log(data[0][0]); // outputs "z"
data[0][0]="x";
console.log(data[0][0]); // still output "z". Shouldn't it show "x"?
What am I missing here?
A two dimensional array is an array, that includes elements that are array's themselves. The example you provided is not a 2D array.
The element in question is in fact a String.
data[0] - Gives you the first element in your data array, which is a string.
data[0][0] - Gives you the first character of this string element.
In JavaScript, a string is a collection of characters, but it isn't an array itself. It can be transformed into a string with string.split('').
Anyways, the reason it shows z instead of x, is because strings are immutable. That means their values can not change. Instead, new objects are created.

Difference between str[0] and str.charAt(0) [duplicate]

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.

Categories