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.
Related
This question already has answers here:
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
Closed 2 years ago.
This code does not work
var x ='hello';
x[2] ='a';
Why doesn't it work?
I am not using the replace() function
In javascript the strings are immutable, you cannot change the string using index.
The string manipulation methods such as trim, slice return new strings.
It doesn't work because of legacy behavior of promoting string values to String object wrappers with the value of the primitive string value.
So for example
"xyz".charAt(0)
is at least notionally evaluated as
(new String("xyz")).charAt(0)
because charAt is a prototype method of String objects and can be applied to a primitive string value in this manner.
The down side is that the promoted String object is never stored anywhere and, even if modified in JavaScript is immediately discarded. This is what is happening in the code posted:
var x ='hello';
x[2] ='a';
is treated as
new String(x)[2] = 'a'
followed by discarding the String object which, incidentally, has a property named 2 with value 'a'.
The general solution to this is to run JavaScript in strict mode, so that any attempt to assign a property to a primitive value which has been promoted to an object wrapper generates an error:
"use strict"
var x ='hello';
x[2] ='a';
This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!
This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 3 years ago.
code snippet of unirest
As you can see above, arrayIndexOf refers to a function. When invoking the arrayIndexOf function,
is it necessary to put "~" before "arrayIndexOf"? I try to change "~arrayIndexOf(value, field)" to "arrayIndexOf(value, field)". And it works as the same? Is there anything I miss?
In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right
before an indexOf() to do a boolean check (truthy/falsy) on a string.
On its own, indexOf() returns the index number of a String object
passed in. So if -1 is returned it will be turned into 0 which is
falsy.
Source: https://wsvincent.com/javascript-tilde/
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();
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.