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");
Add this to your script and press refresh. Nice!
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. If you’re on Chrome you can right click, select inspect, then choose the console tab. You’ll be able to see all kinds of information about your program here and you’ll be using this a lot going forward.
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 functionality. 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. Weird?
Numbers and strings are different from each other. 123 + 456 adds the numbers together/ ‘123’ + ‘456’ glues the strings one after the other. Strings are sequences of letters.