본문 바로가기

Python_WEB/JavaScript

[freecodecamp]JavaScript 문자열 불변성 이해

반응형

JavaScript에서 문자열 값은 변경 불가능합니다.

즉, 생성 된 후에는 변경할 수 없습니다.

 

예>

var myStr = "Bob";
myStr[0] = "J";

 

myStr의 내용을 변경할 수 없기 때문에 myStr의 값을 "Job"으로 변경할 수 없습니다.

이것은 myStr을 변경할 수 없음을 의미하는 것이 아니라 문자열 리터럴의 개별 문자를 변경할 수 없음을 의미합니다.

myStr을 변경하는 유일한 방법은 다음과 같이 새 문자열로 할당하는 것입니다.

 

var myStr = "Bob";
myStr = "Job";

 

Q>

1. 위의 예에 표시된 접근 방식을 사용하여 Hello World의 문자열 값을 포함하도록 myStr에 대한 할당을 수정합니다.

2. myStr은 Hello World의 값을 가져야합니다.

3. 지정된 주석 위의 코드를 변경해서는 안됩니다.

 

A>

// Setup
var myStr = "Jello World";

// Only change code below this line
// myStr[0] = "H"; // Change this line
// Only change code above this line
myStr = "Hello World";

 

https://www.freecodecamp.org/

 

freeCodeCamp.org

Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

www.freecodecamp.org

 

반응형