Common Methods - JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

Chapter 17. Common Methods

String Methods or Commands

In the web, manipulation or processing text is an important task. Just knowing how to process text well in JavaScript can make you become a valuable web developer. So, here are some methods, commands, or keywords that can help you manage string data.

search() method

The search() method allows you to check if a certain string exists within another string. You can also use search() to give you the exact location the string that you want to find in another string. For example:

<body>

<p id = "as" >The string isThistheText? exists in this string."</p>

<script>

var searchQuery = "isThistheText?";

var article = document.getElementById('as').innerHTML;

if (article.search(searchQuery)) {

alert(searchQuery + " is present on the string: " + article)

}

else {

alert(searchQuery + " is not present on the string: " + article);

}

</script>

</body>

If the value of the search() method returns more than 1, it will be considered as true. The search() method returns 0 if the string being searched do not exist. Do remember that any number aside from 0 is considered True. And the number 0 is False.

On the other hand, if you just want to find the location of the string, you can just use search and the number it returns. For example:

<body>

<p id = "as" >The string isThistheText? exists in this string."</p>

<script>

var searchQuery = "isThistheText?";

var article = document.getElementById('as').innerHTML;

var position = article.search(searchQuery);

alert("The string " + searchQuery + " starts at character " + position + ".")

</script>

</body>

Having knowledge about the position of the string you are searching can allow you to perform other useful functions at the string.

Take note: the search() will only return the first instance of the string you are searching. For example:

<body>

<p id = "as" >The string isThistheText? exists in this string. isThistheText? "</p>

<script>

var searchQuery = "isThistheText?";

var article = document.getElementById('as').innerHTML;

var position = article.search(searchQuery);

alert("The string " + searchQuery + " starts at character " + position + ".")

</script>

</body>

This example code will always return 11 even if you keep adding another instance of the searchQuery at the end or at any position after the first instance of searchQuery,

Array Methods and Properties

Arrays in programming are usually treated as objects — it is true in JavaScript. And in JavaScript, arrays have built-in properties and methods that can help you manage and manipulate arrays better and easier.

Length

One of the properties that you will be using a lot when handling arrays is length. The length property allows you to know how many data or elements are within an array. For example:

var exampleArray = ["Google", "Yahoo!", "Bing"];

alert(exampleArray.length);

In this example, the message box will contain the number 3 because there are three values inside exampleArray.

Dot Accessor Operator

The dot operator is JavaScript’s accessor operator. The accessor operator allows programmers to access the properties and methods of an object. Since arrays are objects, you can use the dot accessor to access its properties and methods such as length. The syntax for using the dot accessor is: object.properties.

Push

Push is an array object method. What it does is that it allows you to add another value in an existing array. For example:

var exampleArray = ["Google", "Yahoo!", "Bing"];

alert(exampleArray.length);

var exampleArray.push("DuckDuckGo");

alert(exampleArray.length);

As you will see, the values within the exampleArray will increase by one.