Strings · Cadenas
Strings are text
- A string is text inside quotes:
"Ada"or'Ada'. - Join strings with
+— this is called concatenation.
Las cadenas son texto
- Una cadena es texto entre comillas:
"Ada"o'Ada'. - Une cadenas con
+; esto se llama concatenación.
let first = "Ada";
console.log("Hi, " + first + "!"); // → Hi, Ada!
Mind the spaces
+joins text exactly. It never adds a space for you.- Put the space inside the quotes, where you want it to appear.
Cuidado con los espacios
+une el texto exactamente. Nunca añade un espacio por ti.- Coloca el espacio dentro de las comillas, donde quieras que aparezca.
let pet = "Rex";
console.log("Good dog," + pet + "!"); // → Good dog,Rex! (no space)
console.log("Good dog, " + pet + "!"); // → Good dog, Rex!
Length and capitals
.lengthcounts the characters..toUpperCase()and.toLowerCase()return a copy in CAPITALS or small letters.
Longitud y mayúsculas
.lengthcuenta los caracteres..toUpperCase()y.toLowerCase()devuelven una copia en MAYÚSCULAS o minúsculas.
console.log("hello".length); // → 5
console.log("hello".toUpperCase()); // → HELLO
Index and slice a string · Indexar y rebanar una cadena
Characters are numbered from 0. A slice takes from start up to — but not including — end. · Los caracteres se numeran desde 0. Una rebanada toma desde start hasta — pero sin incluir — end.
Reach inside a string
- Each character has a position, counting from 0:
text[0]is the first one. .slice(start, end)takes the part fromstartup to — but not including —end.
Acceder al interior de una cadena
- Cada carácter tiene una posición, contando desde 0:
text[0]es el primero. .slice(start, end)toma la parte desdestarthasta —pero sin incluir—end.
let word = "JAVASCRIPT";
console.log(word[0]); // → J
console.log(word.slice(0, 4)); // → JAVA
Watch out
- Positions start at
0, so the 4th character isword[3]. - String methods return a new string — the original never changes.
Ten cuidado
- Las posiciones empiezan en
0, así que el 4º carácter esword[3]. - Los métodos de cadena devuelven una cadena nueva; la original nunca cambia.
Common mistakes
- Index from
0;s.lengthis a property (no()). - Strings cannot be changed in place.
Errores comunes
- Índice a partir de
0;s.lengthes una propiedad (no lleva()). - Las cadenas no se pueden modificar in situ.
Now you try
- Build and inspect strings.
- Press Check answer to test it.
Ahora tú lo intentas
- Construye e inspecciona cadenas.
- Pulsa Comprobar respuesta para probarlo.
let name = "Sam". Use + to log Hello, then the name then !, so it reads Hello, Sam!. · let name = "Sam". Usa + para registrar Hello, seguido del nombre y luego !, de modo que lea Hello, Sam!.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Log how many characters are in the string computer using .length (it is 8). · Registra cuántos caracteres tiene la cadena computer usando .length (es 8).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Log the string cat in CAPITALS using .toUpperCase() (it is CAT). · Registra la cadena cat en MAYÚSCULAS usando .toUpperCase() (es CAT).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
let word = "code". Log just its first · primero letter using word[0] (it is c). · let word = "code". Registra solo su letra primera usando word[0] (es c).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.