How will you find number of characters in the Javascript string text given below?
let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

This question was previously asked in
Beltron Programmer 1 Oct 2023 Official Paper
View all BELTRON Programmer Papers >
  1. let length = len(text);
  2. let length = 26:
  3. let length = strlen(text);
  4. let length = text.length;

Answer (Detailed Solution Below)

Option 4 : let length = text.length;
Free
Beltron Programmer Mock Test
0.8 K Users
20 Questions 20 Marks 24 Mins

Detailed Solution

Download Solution PDF

The correct answer is Option 4) let length = text.length;.

Key Points

  • In JavaScript, length is a built-in property used to find the number of characters in a string or the number of elements in an array.
  • To find the length of a string, you can use the syntax: string.length.
  • Given the code: let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";, the correct way to get the number of characters is:
    let length = text.length;
  • This returns 26 because the string contains all 26 uppercase letters of the English alphabet.

Additional Information

  • len() and strlen() are functions used in Python and C respectively, not JavaScript.
  • Option 2 assumes the string always has 26 characters, which may not always be true. You should calculate length dynamically using .length.
  • This property is extremely useful when performing string validations, slicing, or loops based on string size.

Conclusion: text.length is the correct and standard way to determine the number of characters in a JavaScript string.

Latest BELTRON Programmer Updates

Last updated on Nov 25, 2024

-> BELTRON Programmer 2024 Notification has been released on the official website.

-> The Bihar State Electronics Development Corporation Limited (BELTRON) has announced a recruitment drive for Programmer positions on a contractual basis.

-> Specific vacancy details will be shared separately.

-> Interested candidates can apply online from November 11, 2024, to December 10, 2024.

-> The Minimum age of the candidates should be 21 years and maximum age should be 59 year of age. 

Get Free Access Now
Hot Links: teen patti sweet all teen patti teen patti - 3patti cards game downloadable content