Swapna Kumar Panda Profile picture
| Tech Writer, Educator | prev. Architect | Python, JavaScript, SQL | Programming, Development, Databases, AI Tools, Remote Jobs | Building @JabardastDEV |

Dec 7, 2021, 13 tweets

💜 Strings in JavaScript

String
↳ Primitive
↳ String Literals
↳ Single Quotes
↳ Double Quotes
↳ Back Tick
↳ String()
↳ Object
↳ new String()

Full Details Inside.

Let's explore

1️⃣ Primitive vs Object Type
2️⃣ Auto Convertible
3️⃣ Immutable
4️⃣ Easily Comparable
5️⃣ Array-Like
6️⃣ Spreadable
7️⃣ Possible to Destructure

1️⃣ Primitive vs Object Type

✪ In JavaScript, a string value can either be a primitive or, an object.

✪ What is Primitive?
A primitive is a data that is not an object and has no methods.

1️⃣.1️⃣ String Primitives

✪ String Primitives are created either by using String Literals or, String function [String()].

✪ A String Primitive has a separate data type: "string"

1️⃣.1️⃣.1️⃣ String Literals

✪ What is a String Literal?
String literals can be specified using single quotes ( ' ) or double quotes ( " ) or back tick characters ( ` ).

Example: 👇

1️⃣.1️⃣.2️⃣ String Function

✪ String function can be called in a non-constructor context (i.e., without using the new keyword).

✪ This basically is used to convert data of other types to string.

✪ After conversion, it returns a string literal.

Example: 👇

1️⃣.2️⃣ String Objects

✪ String primitives have a wrapper equivalent.

✪ String objects can be created using constructor of String type i.e., new String()

✪ It is mostly discouraged to create String objects manually as they mostly operate as wrappers of primitives.

2️⃣ Auto Convertible

✪ 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 to a String object.

✪ And then call the method or perform the property lookup on the object.

3️⃣ Immutable

✪ By immutable, it means the original value (or, content) cannot be altered.

✪ Strings are immutable in JavaScript.

✪ None of the methods or, properties can alter the original string. When required to alter, they return a new string.

Example: 👇

4️⃣ Easily Comparable

✪ In JavaScript, string literals can easily be compared using comparison operators like ==, !=, <, <=, >, >=.

Example: 👇

5️⃣ Array-Like

✪ An Array-Like looks like an Array. It has a length property. It's iterable. And, its elements can be accessed using indexes.

✪ In JavaScript, strings are not real arrays, rather just array-like.

Example: 👇

6️⃣ Spreadable

✪ Like an array, a string can also be used along with spread operator [...]

✪ Spread Operator Assignment returns a new array of all characters in the string.

Example: 👇

7️⃣ Possible to Destructure

✪ Like an array, a string can also be destructured.

Let's see 👇

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling