0

How should an array of chars or a string be declared? Not sure if there's a difference between array of chars and string either.

let operators = new String();

or

let operators = '';
doe
  • 445
  • 6
  • 17
  • A `String` is an object, while a `string` is a primitive data type. hey are often usable interchangeably, but not always. Stick with the primitive and avoid `String` unless you need it. You can also use `String()` instead of `new String()` to get a primitive. See [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Distinction_between_string_primitives_and_String_objects) for more information. – jcalz Dec 31 '19 at 02:32
  • JavaScript doesn't have "characters" either, only strings. So `"foo".charAt(0)` returns the *string* `"f"`. There's not much point to using an array of "characters" for a string: an array of strings representing a single string is extra hassle for no benefit. Stick with `string`. – jcalz Dec 31 '19 at 02:33

2 Answers2

1

There is no array of chars in javascript and so in typescript (which is a super set over javascript).

'' is a String object. And it's called a literal form.

['a', 'b', 's'] is an array of String objects.

So just use '';

Now what is the difference between '' and , String. The first is a primitive and the second is an object. But whatever whatever they are the same as for the first JavaScript automatically wrap it and construct a String object for it. And so we can use all the method of the string object! Except for when it's used with eval(). The primitive is treated as a code source. And the String object is treated as a string. And a primitive will still differentiated from an object. But as functional they are the same.

Here a show of difference with console.log()

enter image description here

From a general programming side! They are the same! And the difference doesn't matter at all. In few cases! As like with eval or console.log. The default handling of the engine can be different. But again whatever whatever rarely you get on those cases. And now it's all clear. The doc show more and better read the quote bellow.

Better look at the doc ref here a link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Quoting:

Note that JavaScript distinguishes between String objects and primitive string values. (The same is true of Boolean and Numbers.)

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (that is, called without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

let s_prim = 'foo'
let s_obj = new String(s_prim)

console.log(typeof s_prim) // Logs "string"
console.log(typeof s_obj)  // Logs "object"

String primitives and String objects also give different results when using eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object. For example:

let s1 = '2 + 2'              // creates a string primitive
let s2 = new String('2 + 2')  // creates a String object
console.log(eval(s1))         // returns the number 4
console.log(eval(s2))         // returns the string "2 + 2"

For these reasons, the code may break when it encounters String objects when it expects a primitive string instead, although generally, authors need not worry about the distinction.

A String object can always be converted to its primitive counterpart with the valueOf() method.

console.log(eval(s2.valueOf()))  // returns the number 4
Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
0

For me, I usually do let operators = ''; to declare and initialize the string variable.

John Arc
  • 173
  • 1
  • 17