var str = new String(); // Currently empty!
alert( str.length ); // 0
alert( "Hello".length ); // 5
Note: the Array object also has a length property, which returns the number of elements.
var str = document.getElementById("comments").value;
if( str.length == 0 ) // Check for empty string
alert( "Please enter some comments!" );
var str = "I learn JavaScript";
// Get the last character:
alert( str.charAt( str.length-1 ) ); // 't'
Note: the charAt() method returns the letter at the character position index passed as argument.
var str = document.getElementById("comments").value;
if( str.length ) // true if length > 0
document.commentForm.submit(); // Send their feedback
else // Empty string's length = zero = 'false'
alert("Please enter your comments.");
$str = 'I learn PHP';
echo strlen( $str ) . ' characters found in that string'; // 11