Create a new page from your HTML template and save it as string.html. You should have a script element in the HTML. If there is a function or anything else inside it get rid of that to leave just the script opening and closing tags with the type attribute in the opening tag.
Inside the script element add this code:
var myString='hello there';
var myStringLength=myString.length;
alert(myStringLength);
Save the page and try it. You should get an alert dialog with 11 in it. Look at the code. First it created a variable and put a string in it. Then it did something with objects. You might remember that the dot operator separates objects from one of three things:
- objects inside it (child objects)
- methods (like functions - code that does something)
- properties (things to describe the object)
To explain the code line by line:
- first some data is put into a variable in the old fashioned way
- now mysteriously that variable has become an object which has a property called length which is the number of characters inside it and we put that into another variable
- then we use an old fashioned function to report the result
In JavaScript if you create a variable and put a string in it the data is actually stored inside a new string object with that name. Although you created a variable in your code JavaScript actually created an object. This is good because string objects in JavaScript have some useful methods which you can use.