1.

let vs var in JavaScript?

Answer»

The length property is used in JAVASCRIPT to get the length of a STRING:

The SYNTAX:

string.length

Let’s say the following is our string:

VAR myStr = "This is an example!";

Now get the length of the string in a new variable:

var res = myStr.length;

The following is an example that displays the string length:

<html>    <head>       <title>JavaScript String Length</title>    </head>    <body>       <script>          var myStr = "This is an example!";          var res = myStr.length;          document.write("Length = " + res);       </script>    </body> </html>

The output:

Length = 19

Let us now see what we will get when we will TRY to find the length of an empty string:

<html>    <head>       <title>JavaScript String Length</title>    </head>    <body>       <script>          var myStr = "";          var res = myStr.length;          document.write("Length = " + res);       </script>    </body> </html>

The output displays 0 since it is an empty string:

Length = 0


Discussion

No Comment Found