Drill down on JScript syntax-constants and variables

Source: Internet
Author: User
Tags define definition constant numeric variables return variable valid
Js|jscript| variable | Syntax Example:

In the previous example:

Return "<font color=gray>" +hit+ "</font>"


Variables and constants are used, "<font color=gray>" and "</font>" are literal constants, and hit are variables.

The plus sign (+) adds the three to one text: "<font color=gray>100</font>" if the hit is 100.

 

Numeric constants

This is simple, the math class should know how to represent numbers, there is nothing special in JScript.

123
0
-100
100.123
1E3 (1000)

Are all legitimate numeric constants.

Text constants

This is used most, because the HTML text is often generated dynamically. Constants must be enclosed in quotation marks:

"ABC"

"123"

"Text Constants"

are all legitimate text constants, why do you use quotes? People who have absolutely no programming experience may be a little confused, actually very simple,
The computer needs to distinguish between constants and variables.

Variable Assignment

A = "abc"

The value of variable A is "ABC". Why to compare with = =, in order to distinguish with the value of assignment!

b = A + "123"

The value of variable B is "abc123". What is the difference between a constant and a variable if it's not quoted?

b = "a" + "123"

The value of variable B is "A123"

 

So what if the text contains quotes? For example, how to say:

<input Type=button value= "a A" >

Because there are spaces in the name of the button, you must enclose it in double quotes, how do you represent the text constant?

JScript can also use single quotes:

' <input Type=button value= ' a ' > '

You can use double quotes directly in single quotes. In turn, you can use single quotes directly in a constant that is enclosed in double quotes.

"<input Type=button value= ' A A ' >"

So what happens when you have two quotes in a constant? This requires the use of the escape symbol backslash \

"ABC ' def\" Ghi "

In a constant, you can use "\" to represent a double quotation mark. And there are:

\ r \ n indicates carriage return
\ \ denotes a backslash

 

 

function MyName (name) {

if (name== "Xiaoqing") {
document.write ("<input type=button value= ' Hide xiaoqing ' onclick=\ ' mytext.style.display= ' None '" > ")
}
Else
document.write (name)

}

The above function, when the input name is "Greenie", the output:

<input Type=button value= ' hidden ' >

 

Variable definition

JScript can be used directly without predefined variables, but this often poses potential problems.
So it is recommended that you use variables before you define them well.

There are two types of variables, defined within a function as local variables.

<script>
function MyFunc () {

The variable definition uses VAR, and the following defines the variable a
var a

You can define multiple variables at once, separated by commas
var i,j,k

You can also assign an initial value to a variable when you define it
var a= "ACB"

var a=123,b= "DDD"

}
</script>

A local variable is valid only within a function, and the variable does not exist if it leaves the function.

If the definition is outside the function, it is a global variable:

<script>
Define a global variable a
var a
</script>

The variable is valid both inside and outside of all functions, unless a variable with the same name is defined within the function

<script>
Define global variable A
var a=100

function MyFunc () {

var a= "ABC"

The function output is ABC, and the variables inside the function mask the global variable with the same name
document.write (a)

}
</script>

If you do not use Var to define, then the variable is a global variable. So if you don't explicitly define variables in use,
You might misuse a global variable, such as:

<script>
Define global variable A
var a= "dddd"

...

function MyFunc () {
The function only wants to use a temporary variable to save the return result
a=b+ ...

...

Return a

}
</script>

Then once the myfunc is executed, the value of the original global variable is corrupted, which may cause the use
The program for this global variable is wrong! It is therefore highly recommended that you define variables explicitly and use as few global variables as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.