MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
  • Home
  • About
  • Contact
  • Privacy
  • Sitemap
  • BLOG
  • AFFILIATE MARKETING
    • LeadsArk
  • MORE
    • MAKE MONEY
      • ONLINE BUSINESS
      • ADSENSE
      • AFFILIATE MARKETING
    • BLOGGING
      • SEO
      • BlogSpot
      • WORPRESS
      • GOOGLE
      • COMPUTER TIPS
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DEVELOPMENT
      • PHP
      • WORDPRESS
  • DOWNLOADS
    • Blogger Template
    • Wordpress Theme
    • PDF

Monday, November 25, 2019

How to use strings in JavaScript?

 MyBlogHelp     November 25, 2019     javascript     1 comment   

JavaScript Strings

 

   JavaScript - Strings

  1. Introduction to JavaScript strings
  2. Creating JavaScript strings
  3. Adding special characters to JavaScript strings

Introduction to JavaScript Strings

A string is an object in JavaScript. The sequence of characters is stored in a string object such as My Blog Help is a string. Strings are defined in double or single quotes.

As you know, JavaScript is used to generate dynamic and interactive web pages. Because of this reason strings have been defined as an object in JavaScript. Having the objects of strings, you can control the presentation of the strings at page loading time according to you.

JavaScript provides you many methods to control string representation on the webpage. With the help of these methods, you can easily perform operations on strings. You will learn further about these.

Creating JavaScript Strings

In JavaScript, you create strings like any normal variable. Its simple syntax has been given below.

var stringName = "text";

As I told you before, you define strings in single or double-quotes. So you can define even strings in single quotes.

var stringName = 'text';

Creating strings in JavaScript is being explained by the example below.

var name = "My Blog Help Tutorials";

Adding Special Characters to JavaScript String

Suppose you want to type a word in double-quotes in the string, for that you will write that text in the double quote as below.

var name = "My"Blog"Tutorials";

But this method is not correct according to JavaScript and in this way, if you add double quotes, you will get an error. Because the string is already in double-quotes so an error will be generated. Similarly, if you try to add a backslash, the error will also appear.

JavaScript provides escape sequence characters to add such type of characters. With the help of these characters, you can create strings without generating an error.
In JavaScript and other languages, escape sequence characters are defined with the backslash (\). With Escape characters, you can add other special characters (@, $, etc) to strings too.

Common JavaScript escape characters are being given below.

Escape Characters                           Description

     '                                            To add a single quote inside String.
    "                                            To add double quotes inside the string.
    b                                            To add backspace inside the string.
    r                                            To add carriage return inside String.
    f                                            To add form feed inside String.

Common String Methods

JavaScript provides you many useful methods to work with the strings on the advance level. All methods are called by the dot operator (.) with the string object. It is important to note here that all strings start with index zero.

Some common JavaScript string methods are being given below.

Javascript Length Property 

JavaScript provides you a length property so that you can know the length of any string. You use this property by using a dot (.) Operator with a string name. Its syntax is being given below.

stringName.length;

An example of this is being given below.

<! - javascriptLengthProperty.html ->


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var MBH="My Blog Help";
// Printing length of string
document.write(MBH.length);
</script>
</body>
</html>

The above script generates the below output.
12

JavaScript string charAt ()

This method returns the available character to the passed position. An example of this is being given below.

<! - charatMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var name="Abdussamad";
// Printing character at position 1
var result=name.charAt (1);
document.write(result);
</script>
</body>
</html>

The above script generates the given output below.
b

JavaScript string Concat()

This method returns to combine two strings. It is called on a single string and the second string is passed in it. An example of this is being given below.

<! - concatMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var fname="Md"
var lname=" Abdussamad";
// concatenating first name and last name
var result=fname.concat(lname);
document.write(result);
</script>
</body>
</html>

The above script generates the given output below.
Md Abdussamad

JavaScript string endsWith()

This method checks whether the string ends with a passed character or string. An example of this is being given below.

<! - endWithMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var MBH="My Blog Help";
// Checking what a string ends with
var result=MBH.endsWith("Help");
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
true

JavaScript string include ()

This method checks whether a passed string or character is in a string or not. An example of this has been given below.

<! - includesMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var name="My Blog Help";
// Checking if a string contains a word
var result=name.includes("Blog");
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
true

JavaScript string indexOf ()

This method returns the position of the passed string. An example of this is being given below.

<! - indexOfMethodExample.html ->


<!DOCTYPE html>
<html>
<script type ="text/javascript">
var mbh="My Blog Help";
// Getting started index of a word
var result=mbh.indexOf("Blog");
document.write(result);
</script>
</body>
</html>

The above script generates the given output below.
3

JavaScript string search ()

This method searches for a passed value to a string. If the passed value is found, then this function returns its position. An example of this is being given below.

<! - searchMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="My Blog Help";
// Searching
var result=str.search("Help");
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
9

JavaScript string replace()

This method replaces the passed string is a string and places a string with the second argument and returns the complete string. An example of this is being given below.

<! - replaceMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var mbh="My Tutorials Help";
// Replacing a word
var result=mbh.replace("Tutorials", "Blog");
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
My Blog Help

JavaScript string substr()

This method returns the substring which extracts from a string. You pass the starting index number of the substring you want to extract as the first index and in the second argument, you pass the number of characters that you want to extract after the starting index.

An example of this is being given below.

<! - substrMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var bht="My Blog Help";
// Getting substring from a string
var result=bht.substr(3,4);
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
Blog

JavaScript string toLowerCase ()

This method converts all the characters of a string into lowercase. An example of this is being given below.

<! - toLowerCaseMethodExample.html ->


<!DOCTYPE html>
<html>
<script type ="text/javascript">
var mbh="My Blog Help";
// Converting string to lowercase
var result=mbh.toLowerCase();
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
my blog help

JavaScript string toUpperCase ()

This method converts all the characters of the string into uppercase. An example of this is being given below.

<! - toUpperCaseMethodExample.html ->


<!DOCTYPE html>
<html>
<script type="text/javascript">
var str="my blog help";
// Converting string to upper case
var result=str.toUpperCase();
document.write(result);
</script>
</body>
</html>

The above script generates the below output.
MY BLOG HELP

   <<-- PREVIOUSE                                                                    NEXT -->>










  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

1 comment:

  1. JohnJanuary 7, 2020 at 12:32 PM

    xz

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Thank you for reading this post. Please do not enter any spam link in the comment box.

Followers

Blog Archive

  • ►  2020 (4)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
  • ▼  2019 (52)
    • ►  December (1)
    • ▼  November (2)
      • How to use functions in JavaScript?
      • How to use strings in JavaScript?
    • ►  October (1)
    • ►  August (1)
    • ►  July (1)
    • ►  May (11)
    • ►  April (10)
    • ►  March (6)
    • ►  February (3)
    • ►  January (16)
  • ►  2018 (54)
    • ►  December (27)
    • ►  November (5)
    • ►  May (5)
    • ►  April (4)
    • ►  February (3)
    • ►  January (10)
  • ►  2017 (17)
    • ►  December (9)
    • ►  November (8)

Popular Posts

  • How to use the main tag in html5?
  • How to use keygen tag in html5?
  • How to use html5 mark tag to highlight text?
  • How to start Online Business without money from home?
  • What is a search engine?

Contact Us

Name

Email *

Message *

About MyBlogHelp

Most of the posts, we share on this blog related to Affiliate Marketing, Organic Lead Generation, Digital Marketing, Social Media Marketing, Make Money Online, SEO, and web design.

Featured Post

LeadArk - Affiliate Marketing | Qualified Organic Lead Generation | LeadsArk Review

Copyright © MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
Design by Md. Abdussamad | MyBlogHelp.com