Javascript Blank/Space


In most cases, JavaScript ignores blanks or whitespace between JavaScript statements (including tab indentation). The usual use of whitespace or tab indentation is only to make the JavaScript program more readable, as in the following example:

<html>
<head>
<script type="text/javascript">
function getAge() {
    var age;
    age = document.getElementById("age_input").value;
    if ( age == "" )
    {
        alert("Please enter Your ageļ¼");
        return false;
    }
    if (age > 25) {
        alert("Your age more than 25 years old.");
    } else if (age < 25) {
        alert("Your age less than 25 years old.");
    } else {
        alert("Your age equal 25 years old.");
    }
}
</script>
</head>
<body>
Your age : <input type="text" id="age_input" />
<input type="button" onclick="getAge()" value="Submit" />
</body>
</html>

In this example, enter a number in the input box, and then click the "OK" button, depending on the size of the number of different messages pop-up message box. This example is somewhat more complex than the previous example, and for the sake of this article and its content, the specific syntax is not explained in detail.

Although this example is more complex, but because of the use of indentation and the appropriate space, making the program very easy to read. Note, however, that whitespace in the middle of the string is not ignored, as in the above example, the blanks in the middle of the string are output as they are.

JavaScript wrapping

Like blanks, there are line breaks (also called carriage returns), which are often separated for readability to separate lines. Javascript is;; to indicate the end of the statement, so in the "statement" can be wrapped, as shown in the following example:

var my_name = "Jack";
var age = 25;
alert("My name is " + my_name + ",I'm "
+ age + "years old.");

However, the string is not for the trip, such as:

var my_name = "Jack";
var age = 25;
alert("My name is " + my_name + ",
 I'm " + age + "years old.");

In this case, an "unterminated string constant" error is indicated. In addition, there is also a line break is not free to use, and that is the return keyword. Return is used to interrupt the execution of a function and return a value. If the return line after the return will return undefined, as shown in the following example:

function testFunc(i) {
    return
    i;
    // should be return i;
}

The simple example above does not return the value of the passed parameter i, but returns undefined, although the statement is not an error, but obviously does not match the expected results.

Note

The newline in this article refers to the line break (return) of the JavaScript code, not to the output line break.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook