Working with strings: [11 of 51]
Strings
- The data type that represents textual elements on the web
- Could be anything from the output of
console.logto text on a web page
Concatenation
- Used to format text before it reaches its final destination
What is String Concatenation?
Combining two or more strings
- Feature that makes it easier to format text
- Can join a combination of variables and/or actual strings (also known as string literals)
- Concatenating strings will make a new string
Done with a + operator
- The addition operator is not just for numbers…
Using the + operator
let str1 = "Hello";
let str2 = "World!";
console.log(str1 + str2);
console.log(str1 + "Big" + str2)
Output:
HelloWorld!
HelloBigWorld
Adding spaces:
let str1 = "Hello ";
let str2 = "World!";
console.log(str1 + str2);
console.log(str1 + "Big " + str2)
Output:
Hello World!
Hello Big World!
Be careful with numbers!
- Numbers and strings both use the + operator
- Always keep everything the same data type
let num1 = 1;
let num2 = "1";
console.log(num1 + num2);
console.log (num1 + 1)
Output:
11
2
from: Working with strings [11 of 51] | Beginner’s Series to JavaScript