Language
string: 連結は元の文字列を変更しない
文字列演算は新しい値を返すため、元の変数が指す文字列そのものは変わりません。
SourcestringConcatenation.ts
Node.js 22
export function appendScript(label: string): string {
return `${label}Script`;
}TeststringConcatenation.test.ts
Node.js 22
import assert from "node:assert/strict";
import test from "node:test";
import { appendScript } from "../../src/string/stringConcatenation.js";
test("文字列の連結は元の値を変更しない", () => {
const label = "Type";
assert.equal(appendScript(label), "TypeScript");
assert.equal(label, "Type");
});01Assertion
このテストで確認できること
- 連結結果は新しい文字列として返る
- 元の文字列は同じ内容のまま残る
文字列は不変です。可変な配列の push と同じ感覚で更新を期待せず、戻り値を使います。
OBSERVED RESULT
expected: label → Type
actual: Type
expected: appendLabel(label) → TypeScript