Demonstrate that all negative numbers are converted to 0 for string substring function in JavaScript

Description

The following code shows how to demonstrate that all negative numbers are converted to 0 for string substring function.

Example


<!--   w  w  w  .  j av a  2  s  . co m-->

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var stringValue = "hello world";

document.writeln(stringValue.substring(-3)); //"hello world"
document.writeln(stringValue.substring(3, -4)); //"hel"

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Demonstrate that all negative numbers are converted to 0 for string substring function in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      String
...
Compare the char value in JavaScript
Compare two strings in JavaScript
Concatenate one or more strings to another ...
Convert Number to String with toString meth...
Convert boolean to String in JavaScript
Convert boolean to string with String() in ...
Convert null to string with String() in Jav...
Convert number to string in radix in JavaSc...
Convert number to string with String() in J...
Convert string to lower case based on local...
Convert string to lower case with toLowerCa...
Convert string to upper case based on local...
Convert string to upper case in JavaScript
Convert undefined to string with String() i...
Convert value to String with toString metho...
Count the words in JavaScript
Create String from null in JavaScript
Create String object by calling its constru...
Demonstrate that all negative numbers are c...
Divide string into several parts with Strin...
Escape a string with escape function in Jav...
Find all occurrences for the sub string wit...
Get sub string with String substr() method ...
Get sub string with string substring method...
Get substring from the end of a string in J...
Get substring from the end with string subs...
Get substring with String slice() method in...
Get the character in the given position for...
Get the character's character code at the g...
Get the length of a string in JavaScript
Get the number of characters in the string ...
Match string using regular expressions in J...
Parse string in scientific to number in Jav...
Repalce a string with regular expression in...
Replace a substring with String in JavaScri...
Replace string with a custom function in Ja...
Replace string with regular-expression in J...
Reverse string using substr in JavaScript
...