What's the difference between a string and an array of characters in Javascript?
Asked Answered
M

6

10

When I checked if these two were equal, they apparently weren't. Can someone explain why?

var string = "Hello";
var array = ['H', 'e', 'l', 'l', 'o'];

Why is (string === array) is false ?

EDIT: This website is fantastic. Such fast help. Thanks guys.

Maturation answered 16/2, 2014 at 7:55 Comment(0)
M
19

Why is (string === array) is false ?

You are using strict comparison (===), which also checks the data type of the values. Obviously a primitive string value is not the same data type as an object, and objects are only truly equal to themselves. Proof:

var foo = [1,2,3];
var bar = [1,2,3];

console.log(foo === bar); // false
console.log(foo === foo); // true

Now, if you were to use loose comparison (==), the following comparison does return true:

console.log([1,2,3] == '1,2,3'); // true

Why? Because the array is converted to a string first, and this happens to result in the same string value. But that doesn't mean that the values are the same (one is still an array and the other a string). That's why you should always use strict comparison.


What's the difference between a string and an array of characters in Javascript?

Strings are not arrays because they are inheriting from different prototypes (*) and hence have different instance methods. For example, arrays have a method join and strings have a method match.

From one point of view, arrays and strings are similar though, because they are both array-like objects.

What does array-like mean? It means that the object has a length property and numeric properties. A string has a length property which gives you the number of characters in the string, and you can access single characters of the string with str[i]. Example:

var arr = ['a','b','c'];
var str = "abc";

console.log(arr.length); // 3
console.log(str.length); // 3

console.log(arr[0]); // a
console.log(str[0]); // a

console.log(arr === str); // false

console.log(typeof str); // string
console.log(typeof arr); // object

*: Actually, there even is a difference between primitive strings and String objects, but I don't want to go too deep here. Technically primitive strings don't have any methods because they are not objects, but in most cases, you can treat primitive strings like they were objects.

Miguelinamiguelita answered 16/2, 2014 at 8:7 Comment(0)
S
2

You are confused with c/c++. In java-script array is another object and string variable is another one. Try reading this

Sturdivant answered 16/2, 2014 at 8:2 Comment(0)
N
2

In JavaScript === is strict equality which compares two values for equality. Neither value is implicitly converted to some other value before being compared. So that's why you have to different objects (String and Array) which cannot be equal and that is why your comparison returns false.

More on that you can find on Strict equality using ===

Nantucket answered 16/2, 2014 at 8:4 Comment(0)
E
2

Although most of answers are correct I want to add few things on top of that. I don't want to repeat, why comparison is false as other have explained it.

Here is one other difference in string and array with same content.

const str = 'Hello';
const arr = ['H','e','l','l','o'];

When we declare vairbale constant with reference data type we can manipulate content.

arr = anotherArray;

will give us error however still we can do

arr[0] = 'S';

array now will look like ['S','e','l','l','o'];

In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.

Eggshell answered 15/3, 2021 at 4:12 Comment(0)
K
1

In Javascript, String and Array data types are not equal.

Kamikamikaze answered 16/2, 2014 at 8:3 Comment(0)
P
0

Strings and arrays act in mostly the same way but are technically different datatypes within Javascript, so a strict comparison === will say it is false.

Prajna answered 1/6 at 3:28 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPeacoat

© 2022 - 2024 — McMap. All rights reserved.