mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): Add interactive examples to String object lesson (#63374)
This commit is contained in:
+5
-1
@@ -5,7 +5,7 @@ challengeType: 19
|
||||
dashedName: what-is-a-string-object-and-how-does-it-differ-from-string-primitive
|
||||
---
|
||||
|
||||
# --description--
|
||||
# --interactive--
|
||||
|
||||
In previous modules you have been used to working with strings literals like this:
|
||||
|
||||
@@ -15,12 +15,16 @@ const greeting = "Hello, World!";
|
||||
|
||||
But JavaScript also has string objects. Both string objects and string primitives are used to handle text but they function differently under the hood. A string object is created using the string constructor function, which wraps the primitive value in an object. Here is how you would create a string object:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
const greetingObject = new String("Hello, World!");
|
||||
|
||||
console.log(typeof greetingObject); // "object"
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
When we use the `typeof` operator we can see that the result is of type object instead of type string. One of the things that you might have been wondering about is how you can use properties like the `.length` property on string primitives.
|
||||
|
||||
When you use the `length` property on a string primitive, JavaScript temporarily wraps the string primitive in a string object, to perform the operation. This is why you can use the `length` property and the different methods like `repeat()`, `concat()`, and `slice()`. These types of methods and properties are referred to as instance methods also properties and static methods. You will learn about how that works in detail in future modules.
|
||||
|
||||
Reference in New Issue
Block a user