Exercise

  • Open up Google Chrome and head over to your inspector tools.
  • Now I want you to create a string variable named ‘text’ containing any text of your choosing.
  • Now create a new variable named ‘startPos’, which contains the result of a search for a term inside your ‘text’ variable (make sure that the search term does exist inside your string).
  • Now create another variable named ‘endPos’, which contains the value of the length of your text variable (make sure you use the property here and do not hardcode the value yourself).
  • Then create one last variable and assign it the result of a substr of your ‘text’ variable, use the ‘startPos’ and ‘length’ variables as your parameters. This should give you a final variable equal to your initial search term.
  • Finally, check your code is working by logging your final variable to the console.

Answer

If you managed to get the desired output, well done. You have successfully merged some of the key concepts of programming that we have learned so far into one exercise. Fantastic work. If you didn’t get there, that’s no problem; these are all new concepts and it’s completely understandable to be a bit overwhelmed at this stage. If you are feeling that way, I encourage you to go back, revisit this chapter again from the start, and just brush up on the topics covered and attempt this again. It’s important for your own learning and understanding that you are putting into practice what we are learning. If you really can’t get this working on your own, then feel free to check out the solution below; but I do encourage all readers to attempt to get there on their own, as you can learn a lot about programming from your mistakes, and it’s important to start to get into the mindset of debugging code and trying to work out where you went wrong.

Solution

var text = “This is a long string”;

var startPos = x.search(”long”);

var length = x.length;

var slicedText = x.substr(startPos, length);

console.log(slicedText);

Other string methods

There are many other string methods that we can make use of in our code. For example:

  • replace() – for replacing a value in a string with another value.
  • toUpperCase() – for converting a string into entirely uppercase.
  • toLowerCase() – for converting a string into entirely lowercase.

We will be making use of some of these functions in the next section of the book, but for now, let’s move on to take a look at one of the most fundamental aspects of any programming language: objects.

Objects

In JavaScript, objects are absolutely everywhere. Pretty much everything in JavaScript is an object. Strings can be objects, numbers can be objects, functions are objects, values are objects, dates are objects, objects are objects. So by now you’ve probably got the message. Objects are everywhere in JavaScript. So, the obvious question is, what’s an object? Well, in programming, objects can be compared directly with an object in real life. Let’s take the example of an animal. An animal has properties, right? Like its name, its colour, its size, its weight, its breed or its age. An animal also has methods: an animal can talk, it can sit, it can roll over, it can eat and sleep. Well, you can define an object in JavaScript that represents this exact same idea. Objects simply contain data about a ‘thing’. Objects are simply variables that contain more than one piece of information. Ever used a website that you have your own profile on? Which contains your email address, password, address, etc? Well, it’s extremely likely that that information will be joined together into an object in the website’s JavaScript code. Objects should contain all of the relevant information about one thing. Now, enough explaining. Let’s see some code and step through it: