• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » String » JavaScript String length Property
Next →
← Prev

JavaScript String length Property

By Dinesh Thakur

JavaScript Length; You will often find it necessary to count characters and words in strings, particularly with strings from form submissions. For example, you might need to count the number of characters in a password to ensure that a user selects a password with a minimum number of characters.

Or, you might have a Web page that allows users to submit classified ads that cannot exceed a maximum number of characters. The String class contains a single property, the JavaScript Length property, which returns the number of characters in a string. To return the total number of characters in a string, you append the JavaScript Length property of the String class to a literal string, variable, or object containing text.

For example, the following code uses the JavaScript Length property to count the number of characters in a variable named country. The document.write() statement prints “The country name contains 18 characters.”

var country = "Kingdom of Morocco"; 
 document.write("<p>The country name contains " + country.length + " characters.</p>");   
The following example of JavaScript Length uses a regular text string. It writes the length of the string variable onto the page.   
<body> 
   <script type="text/javascript"> 
     var myname="John"; 
     document.write("The name has "+myname.length+" characters."); 
   </script> 
</body> 

Notice how the name of the variable is used like an object name here. This is how to get JavaScript to create a temporary String object to use the property. The script writes the result to the page. Because the name has four characters, the JavaScript Length property has a value of 4 here.

The JavaScript Length property will be quite useful when you want to break strings apart to get information or make changes to them, especially if the viewer enters the string and you don’t know its length beforehand. This property reflects the number of characters, including blanks, which it contains. Here are a few examples:

var str1 = "abc" 
var str2 = "a b c" 
var str3 = "\"abc\"" 
document.write(str1.length + "<BR>") 
 document.write(str2.length + "<BR>") 
 document.write(str3.length)   
The output of this script is: 
3 
5 
5 

The index of a string’s last character is equal to its length (number of characters) minus one. The string’s length is the index of the nonexistent character following the string’s last character.

 

You’ll also like:

  1. JavaScript String constructor Property
  2. JavaScript String Format
  3. JavaScript String substring() Method
  4. JavaScript String substr() Method
  5. JavaScript String search() Method
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.