16 C
Jaipur
Tuesday, December 1, 2020

Javascript String Length – Linux Hint

Must read

Vietnam steps up online crackdown, jailed activists at record high: Amnesty International – Latest News

The number of jailed activists in Vietnam has reached its highest on record, according to a tally by Amnesty International, which in a report...

Tesla: LG Chem to double China battery capacity to meet Tesla demand: Sources – Latest News

South Korea's LG Chem Ltd plans to more than double production capacity of battery cells it makes in China for Tesla Inc electric vehicles...

5G Smartphones: Snapdragon 888: Qualcomm’s new flagship 5G smartphone chip to be manufactured by Samsung – Latest News

Qualcomm Inc on Tuesday announced a new flagship smartphone chip that will feature enhanced gaming and photo abilities and will be manufactured by Samsung...

OEM: Addressable market for OEMs likely to be over Rs 10 lakh crore by 2025: Report – Latest News

The growing domestic handset manufacturing market and supportive policies by government have ensured that India steadily builds on its device manufacturing capabilities, and the...

Javascript is a scripting or programming language that is quickly becoming one of the most widely used programming languages in the world. Strings are a crucial part of all programming languages. Programmers often need to use strings to manipulate or manage data. Javascript’s built-in functions or properties for manipulating strings can come in handy. For example, if you are getting some user data in form fields in HTML and you want to restrict the length of a string while showing some data on a webpage, Javascript’s built-in string length property can help you in this case. This article shows you what the string length property in Javascript is and how you can use it in a few different scenarios.

The string length property fetches all the characters included within a string.

Syntax

The syntax for the string length is as follows:

This property simply gives back the total characters available in the string at run time.

Let us try out a couple of examples that use the string length property.

Examples

First, you will see the basic implementation of this property. Later, you will see its application.
Suppose you have the following string:

let str = “Linuxhint is great.”

If you want to know the number of characters in this string, simply apply the string length property as follows:

As you can see, this simply returns the length of the specified string.

You can use this property in multiple places. For example, say you are doing a check-in on an “IF” statement, like the one below:

if (str.length <= 20) {
 console.log(“It’s a short string”);
} else {
 console.log(“It’s a long string”);
}


And, as you can see in the console output, the statement “It’s a short string” is printed. That is great.

You can use this in the conditional statement of the for loop, as well. If you want to iterate from each of the characters in a string and convert every letter into a lower-case letter, but you are not yet aware of how many characters the string has, then you can simply give the str.length property as a conditional statement.

for (let i = 0; i < str.length; i++) {
 console.log(str[i].toUpperCase());
}


As you can see in the output console, every character is shown in the console separately and converted into uppercase letters, as well.

So, this is how you can apply it to a lot of different scenarios according to your needs.

Interesting Fact

Here is an interesting fact for you guys. Let us now try to dodge the string length property by assigning it a numeric value. You will see that it will either print the assigned value or the real length of the string.

So, first, assign it a value

And now, we will try to console the length of the string.


And, as you can see, it does not show the assigned value. It shows that the length of the string or the number of characters in the string are being calculated at run time, and then it displays the output.

Conclusion

In this article, you learned what the string length property is in Javascript and you have seen its applications in a few different examples. I hope this article proved helpful in understanding the string length property and its implementations. You can read on to learn more about Javascript at linuxhint.com.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Vietnam steps up online crackdown, jailed activists at record high: Amnesty International – Latest News

The number of jailed activists in Vietnam has reached its highest on record, according to a tally by Amnesty International, which in a report...

Tesla: LG Chem to double China battery capacity to meet Tesla demand: Sources – Latest News

South Korea's LG Chem Ltd plans to more than double production capacity of battery cells it makes in China for Tesla Inc electric vehicles...

5G Smartphones: Snapdragon 888: Qualcomm’s new flagship 5G smartphone chip to be manufactured by Samsung – Latest News

Qualcomm Inc on Tuesday announced a new flagship smartphone chip that will feature enhanced gaming and photo abilities and will be manufactured by Samsung...

OEM: Addressable market for OEMs likely to be over Rs 10 lakh crore by 2025: Report – Latest News

The growing domestic handset manufacturing market and supportive policies by government have ensured that India steadily builds on its device manufacturing capabilities, and the...

Free Proxy List 2020 [Proxy Server List To Hide Your IP Address]

With the internet becoming a hotbed for tracking activities and an ever-growing race to collect data, it has become essential to find a means...