String Length in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

The string length property in JavaScript returns the length of string in UTF-16 code units. It returns 0 when the string is empty, it's length. JavaScript uses a UTF-16 string format to depict the most common characters, consisting of a 16-bit data unit. The value returned by length does not match the number of characters in the string. When a string is empty, its length is 0.

Syntax

The syntax of string length property in JavaScript is:

Where string is the name of the string. The string length property in JavaScript doesn't take any parameters.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Value

The string length property will return the number of characters present in the string.

Examples

JavaScript Code to Show the Working of string.length Property

Output:

Assigning to length

Output:

Browser compatibility

Browserlength
Chrome1
Edge12
Firefox1
Internet Explorer3
Opera3
Safari1
WebView Android1
Chrome Android18
Firefox for Android4
Opera Android10.1
Safari on iOS1
Samsung Internet1.0
Deno1.0
Node.js0.10.0

Conclusion

  • The string length property in JavaScript determines the length of the string, that is, the number of characters it contains.
  • Internally, to calculate the number of characters, it counts the number of code units occupied by the string.
  • In the case of an empty string, it returns 0.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more