Don't string me along…
"I am a String"
We can also use single quotes too like so:
'I am a String'
We can add strings together to make longer strings using the plus character:
"Hi there " + "hello"
If we need to know what's inside a string we can find out using an alert box like so:
alert("Hi there " + "hello");
We can also get data out using the console object. We write:
console.log('Hi from the console');
The console is a developer tool. On windows you can open it by pressing F12.
Exercise - Let's alert some strings
Nice and simple this one. We're just going to create some strings and pop them up in little boxes.
- Put the phrase "Hello there Java" + "Script" in an alert box.
Strings are objects
Objects are little balls of related data and ability. We can access information stored in an object using a dot (.).
Let's get the length of the string. We can do it like this:
"Allo allo allo".length
We might even alert this:
alert("Allo allo allo".length)
Exercise - Monkey with Strings
- Write a program that tells me the the number of characters in your name.
- Tell me what 123+456 is. Now tell me what "123" + "456" is.