String Concatenation & Interpolation
To måder at bygge tekst på: concatenation samler strings med plus, mens interpolation indsætter værdier direkte i teksten.
ts · 7 lines
ts
1// section2const firstName = "John";3const lastName = "Doe";4 5const fullName = firstName + " " + lastName;6 7console.log(fullName);ts · 7 lines
ts
1// section2const firstName = "John";3const age = 25;4 5const message = `Hej ${firstName}, du er ${age} år gammel`;6 7console.log(message);csharp · 7 lines
csharp
1// section2string firstName = "John";3int age = 25;4 5string message = $"Hej {firstName}, du er {age} år gammel";6 7Console.WriteLine(message);Hvornår bruges hvad?
Concatenation bruges når du blot skal samle få strings.
Eksempel:firstName + " " + lastName
Interpolation bruges når teksten indeholder flere variabler og skal være mere læsbar.
Eksempel:Hej ${name}, du har ${points} point