Skip to main content

JavaScript String Methods

JavaScript String Methods
Hello there. Welcome to another interesting lesson on JavaScript. Here we shall be looking at string methods. Before going any further, if you are new to this our amazing platform, please do well to check on our previous lessons so as to be on track. We all know what strings are right? If you have forgotten, no need to panic. We got you covered.

Strings are a fundamental part of all programming languages. In JS, a string is any text inside double or single quotes. They store a series of characters like “Trycoder”, “JavaScript”, and so on. Hence string methods assist you in carrying out a variety of string operations. 

There are so many string methods that are available in JS such as concat( ) that joins strings together, indexOf( ) that returns the index of a specified text that is found in a given string, replace( ) which replaces a specified value with another value in a string, etc. In this lesson, we shall take a look at a few of them. 

Let us consider the case of string concatenation. We will be making use of the concat( ) method on two stings below.

var string1 = “I am a student, and I am ”;
var string2 = “learning javaScript”;
var string3 = string1.concat(string2);

The code gives rise to the following output.
JavaScript String Methods
You can see clearly from the output that the two strings have been concatenated (in other words, joint together). This is an example of how the concat( ) string method is being used.

Let us go further by looking at another example. In this case, let’s make use of the replace( ) method to replace “student” with “ programmer”. Consider the following code

var string1 = “I am a student, and I am ”;
console.log(string1.replace(“student”, “programmer”));

Let us see what the code does.
JavaScript String Methods
There you go. Isn’t this exciting? You get to see the beauty of JavaScript as we get further into the course. 

Another very important and most common string method is the .length that returns the length of the string. Let us try to get the length of our first string.

var string1 = “I am a student, and I am”
console.log(string1.length);

The output is seen below.
JavaScript String Methods
Is it working? It’s working. This means our string has 24 characters, including inline spaces.

So we have come to the end of this session. I hope it was an exciting and very educative lesson. For more, stay tuned to this platform. We wish you the best as you code.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...