Asked  7 Months ago    Answers:  5   Viewed   36 times

Is there any reason I should use string.charAt(x) instead of the bracket notation string[x]?



Bracket notation now works on all major browsers, except for IE7 and below.

// Bracket Notation
"Test String1"[6]

// charAt Implementation
"Test String1".charAt(6)

It used to be a bad idea to use brackets, for these reasons (Source):

This notation does not work in IE7. The first code snippet will return undefined in IE7. If you happen to use the bracket notation for strings all over your code and you want to migrate to .charAt(pos), this is a real pain: Brackets are used all over your code and there's no easy way to detect if that's for a string or an array/object.

You can't set the character using this notation. As there is no warning of any kind, this is really confusing and frustrating. If you were using the .charAt(pos) function, you would not have been tempted to do it.

Tuesday, June 1, 2021
answered 7 Months ago

There is no "better" but the more common one is ||. They have different precedence and || would work like one would expect normally.

See also: Logical operators (the following example is taken from there):

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;
Tuesday, June 1, 2021
answered 7 Months ago

No, string primitives do not have methods. As with numeric primitives, the JavaScript runtime will promote them to full-blown "String" objects when called upon to do so by constructs like:

var space = "hello there".indexOf(" ");

In some languages (well, Java in particular, but I think the term is in common use) it's said that the language "boxes" the primitives in their object wrappers when appropriate. With numbers it's a little more complicated due to the vagaries of the token grammar; you can't just say

var foo = 27.toLocaleString();

because the "." won't be interpreted the way you'd need it to be; however:

var foo = (27).toLocaleString();

works fine. With string primitives — and booleans, for that matter — the grammar isn't ambiguous, so for example:

var foo = true.toString();

will work.

Wednesday, June 2, 2021
answered 7 Months ago

Instead of splitting on /[+(-)% */]/ split on more than one: /[+(-)% */]+/. You still might get empty matches at the start and end. To solve that problem you can use a similar regex with replace:

formula.replace(/^[+(-)% */]+|[+(-)% */]+$/g, "").split(/[+(-)% */]+/)


var formula = "(field1 + field2) * (field5 % field2) / field3";
console.log(formula.replace(/^[+(-)% */]+|[+(-)% */]+$/g, "").split(/[+(-)% */]+/));


Wednesday, September 1, 2021
Derek 朕會功夫
answered 3 Months ago

I imagine a constants class would be more efficient.

However, speed shouldn't really be an issue in either case. I would recommend organizing based on what makes sense.

Constants class

Put strings constants that will be used internally, like database column names or other keys.


Put strings that are displayed for the user. This way you can take advantage of localization, etc.

Tuesday, September 7, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :