Local/global variables of JavaScript variables

Source: Internet
Author: User
Tags numeric logical operators

Local JavaScript variable

A variable declared inside a JavaScript function (using VAR) is a local variable, so it can only be accessed within the function. (The scope of the variable is local).
You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.
The local variable is deleted as soon as the function completes.

Global JavaScript variables

Variables declared outside a function are global variables and can be accessed by all scripts and functions on the Web page.

The lifetime of a JavaScript variable

The lifetime of JavaScript variables begins when they are declared.

Local variables are deleted after the function is run.

Global variables are deleted after the page is closed.

Assigning values to an undeclared JavaScript variable

If you assign a value to a variable that has not yet been declared, the variable is automatically declared as a global variable.
This statement:

Carname= "Volvo";

A global variable, carname, is declared even if it executes within a function.

Add:

Variable

For the understanding of variables: variables are the code for the data. Like the name is the same.

var num;//uses keyword var to declare a variable in JavaScript

In JavaScript, using the syntax above, you can declare a variable so that it is given a value later.

var num=123; This is a direct declaration of a variable, and assigned a value, the process of assigning a value is actually a type of variable

var num;

num=123;

Look at the following two code:

The code is as follows Copy Code

document.write (123);

document.write (123);

document.write (123);

document.write (123);

If we use variables to manipulate:

The code is as follows Copy Code

var num;

num=123;

document.write (num);

document.write (num);

document.write (num);

document.write (num);

Although two do not seem to save much ink, but there is a concept is: the use of document.write multiple output, the first output is every time in the memory space to create a new 123 this value. And the second output is the value of that variable. This is what we want, using variables to clearly express what we want to do, and a short variable name reference can make the code clearer and less error-prone! Of course, the name can not be random, variable naming some restrictions: can only contain letters, numbers and underscores, there is a special $ character, and variable names can only be letters, underline, $ start.

Value, type

The code is as follows Copy Code

var dog;

Dog= "Little Tiger";//String

var num;

num=1;//Digital

var strnum;

strnum= "1"//String

var badnum;

badnum=3.345;//Decimal

badnum=.2;//is still a decimal

Very troublesome, can simply write:

var dog,num,strnum,badnum;

Dog= "Little Tiger";//String

num=1;//Digital

.........................

You can see that JavaScript is a dynamic type language, and when you declare a variable without specifying its type, the value of the variable can have a different type at run time.

The data types are:

Strings, Numbers, Boolean values, Undefined, (compound) objects and arrays

Mathematical Operations and comparisons

Primary School Data operations: +,-, *,/,++,--It goes without saying that every language is the same standard. Here is the +, the link string also uses the "+" number.

The code is as follows Copy Code

var num=23+ "12";

Alert ("23+12=" +num);//The result is 2312.

Comparison operator: <,>,<=,>=,==,!=,!, the comparison operator returns a Boolean value that's not much.

logical operators

Logical operators are used to compare Boolean values && | | !

The above operator is used for numeric and Boolean values (+ excluding some)

Getting Started with type conversions

Because JavaScript is a weakly typed language, it is perfectly possible to add strings and numbers (two different types of variables). Here is an example that has been demonstrated.

  code is as follows copy code

var str= "some string here!";

var num=123;

alert (NUM*STR);//The Nan to output, because the multiplication operator can only be numeric, so the computer converts a string to a number when the operation is performed, but when STR converts to a number, it is Nan.

Nan is a special value that does not mean a number, is not a digit, and gets the value when the other fails to convert to a number.

Str= "2";

alert (NUM*STR);//will output 246 because STR can be parsed into the number 2.

Other types of conversions

var bool=true;

alert (bool*1);//Output 1, Boolean true to the number is 1, in fact, the simplest way to convert other values to numbers is to multiply by 1

Bool=false;

alert (bool*1);//Output 0

Alert (bool+ "");//output is false, in fact, the easiest way to convert other values to strings is to write them into an empty string.

var str= "some string";

Alert (!! STR);//true because the non-operator is an operation on a Boolean value, so converting its type requires only two non operations.

Str= "";

Alert (!! STR)//Output False, only the empty string converted to a Boolean value would be false, and a non-empty string to be replaced by a Boolean value is true.

var num=0;

Alert (!! num);//false;

num=-123.345;

Alert (!! num);//true,c is true if any number other than 0 is converted to a Boolean value

Another important thing is to convert the empty string into a number that will be 0;

Alert ("" *1);//Output 0

Get variable type typeof operator

  code is as follows copy code

var bool = True
Alert (typeof bool);//Output Boolean
var num =123
Alert (typeof num);//output number
var str = "some string here"; alert (typeof str);/output string
var strnum = "123";
Alert (typeof strnum);/output String
Strnum *= 1;
Alert (Typ EOF strnum)//output number

Related Article

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.