Read characters and the length of a string.

string-length A string is indexed like a sequence of characters. `length` counts characters, and bracket access reads a character at a position.

String Length

word
string_length.js
Replay: real traced execution (multi-file project)
const word = "trace";
const size = word.length;
const first = word[0];
const last = word[word.length - 1];

console.log("word=" + word);
console.log("size=" + size);
console.log("edges=" + first + last);
const word = "replay";
const size = word.length;
const first = word[0];
const last = word[word.length - 1];

console.log("word=" + word);
console.log("size=" + size);
console.log("edges=" + first + last);
const word = "JS";
const size = word.length;
const first = word[0];
const last = word[word.length - 1];

console.log("word=" + word);
console.log("size=" + size);
console.log("edges=" + first + last);
  1. word ← trace, size ← 5, word.length ← 5, first ← t, word[0] ← t

    1const word→ trace = "trace"; //@word="replay", "JS"2const size→ 5 = word.length→ 5;3const first→ t = word[0]→ t;4const last→ e = word[word.length - 1]→ e;56console.log("word=" + wordtrace);7console.log("size=" + size5);8console.log("edges=" + first→ t + laste);
    outputword=trace
    size=5
    edges=te
  1. word ← replay, size ← 6, word.length ← 6, first ← r, word[0] ← r

    1const word→ replay = "replay";2const size→ 6 = word.length→ 6;3const first→ r = word[0]→ r;4const last→ y = word[word.length - 1]→ y;56console.log("word=" + wordreplay);7console.log("size=" + size6);8console.log("edges=" + first→ r + lasty);
    outputword=replay
    size=6
    edges=ry
  1. word ← JS, size ← 2, word.length ← 2, first ← J, word[0] ← J, last ← S

    1const word→ JS = "JS";2const size→ 2 = word.length→ 2;3const first→ J = word[0]→ J;4const last→ S = word[word.length - 1]→ S;56console.log("word=" + wordJS);7console.log("size=" + size2);8console.log("edges=" + first→ J + lastS);
    outputword=JS
    size=2
    edges=JS