Home 06: Variables and strings
Content
Cancel

06: Variables and strings

Now that you learned how variables can help you to dynamically use numbers inside your program, we will have a look what else we can do with variables.

Letters, words, sentences and paragraphs, in short every type of text, is called a string in most programming languages. Strings can contain every letter, spaces, symbols, and emojis. And like numbers, you can save a string inside a variable. To achieve that, we first have to take a look how strings are created. In JavaScript, you use single or double quotation marks to define a string.

We will use the double quotation marks throughout the course.

You start with a quotation mark ", write your symbol, text, emoji and end the string with another quotation mark ".

1
let emojiName = "Klaus";

Obviously, you cannot calculate with strings, but you can still use the + operator to combine strings.

1
2
3
let emojiName = "Klaus";
let attribute = "awesome";
let sentence = emojiName + " is " + attribute;

And you can use the console.log command to print out your sentence:

1
console.log(sentence);

Escaping in string

If you must use the double quotation marks inside your string, you can escape them. That means they will not be executed like code, but instead they are interpreted as usual characters inside your string. To achieve that, you have to use the escape character \.

1
2
let escapedString = "\"NMD\" stands for New media design";
console.log(escapedString);

Another solution is to use the single quotation marks instead of the double ones:

1
2
let escapedString = '"NMD" stands for New media design';
console.log(escapedString);
This post is licensed under CC BY 4.0 by the author.
Contents