tags: javascript
In Javascript, variables are containers that we can place data inside and store for later. Different kinds of data are treated in different ways in Javascript, the first data type we'll look are are strings:
Characters are the representation of a signle, numbner, letter, or symbol inside of a string. To use an analogy, letters are to words what characters are to strings roughly speaking.
All of these are characters:
"t" // Letter that is a char
"1" // The number one as a char
"%"
"9"
A string is a sequence of characters stored together, you represent a string in Javascript by placing the characters inside of single quotes, double quotes, or back ticks.
var myName = "Michael";
var mySingleQuotedName = 'Michael';
var myBackTickName = `Michael`;
You can also store numbers and symbols inside of strings, not only letters:
var myNumber = "12345";
var mySymbols = "@$^%^*&*#{{CONTENT}}quot;
String Combination
Let's say we have two variables that are strings, each holding part of a sentance that we want to combine:
var sentence1 = "The cat"
var sentance2 - "in the hat."
Let's combine these two sentances into a single sentance to form a third variable:
// Output: "The cat in the hat."
var sentance3 = sentance1 + " " + sentance2;
Combining string in this way is called string combination, we can use backticks in order to do the same thing
var sentance3 = `${sentance1} ${sentance2}`
With backticks, you can use Javascript code inside of strings by using ${} inside of the string, whatever you place inside will be run as Javsacript code, since we put our variables in there, it spits out the output!
String Methods
Strings have built in utility functions called methods,