JavaScript variable data type typeof

Source: Internet
Author: User

The ECMAScript language draws heavily on the syntax of C and C languages (Java and Perl). So developers who are familiar with these languages will feel relaxed when they accept ECMAScript's more liberal syntax.

Case sensitive

The first concept to understand is that everything in ECMAScript (variables, functions, operators) are case-sensitive. This means that test and test represent two different variables, and the function name cannot be used typeof because it is a keyword. And typeof can be a valid function name entirely.

Identifier

The so-called identifiers are variables, functions, names of properties, or parameters of functions. The specification for identifiers is:

The first character must be a letter, an underscore, or a dollar sign;

Other characters can be letters, underscores, dollar signs, or numbers.

By convention, the ECMAScript identifier is in hump case format, that is, the first letter is lowercase. But there is no compulsion to use this approach.

Comments

One-line comment is two double slash//

Multi-line annotations are

/*

*/

Strict mode

Strict mode can be used with ' use strict '

Statement

The statement in ECMAScript is terminated with a semicolon, although the semicolon at the end of the statement is not required, we recommend that you do not omit him at any time.

Variable

ECMAScript variables are loosely typed, and so-called loose types can be used to hold any type of data. In other words, each variable is a placeholder for a saved value. When you define a variable, you also declare it with VAR, followed by the variable name. Such as

VAR message

This line of code defines a variable named message that can be used to hold variable values (such as uninitialized variables, which hold a special value undefined)

<!Doctype HTML><HTML><Head><title>Variable</title><MetaCharSet= "Utf-8" /></Head><Body>   <Script>     varmessage;   alert (message); </Script></Div></Body></HTML>

Using the var operator to define a variable becomes a local variable in the scope that defines the variable. That is, if you define a variable with var in the function, the variable will be destroyed after the function exits.

<!Doctype HTML><HTML><Head><title>Variable</title><MetaCharSet= "Utf-8" /></Head><Body>   <Script>    functionTest () {varmessage; alert (message);//Eject undefined} test (); alert (message);//Error   </Script></Div></Body></HTML>

The variable message is defined in the function with Var, when the function is called, and the variable is created and assigned a value. After this, the variables are destroyed immediately. So the next line of code in the example above will be an error. However, when declaring a variable, you can omit Var so that it becomes a global variable without an error. So as soon as the function is called, the variable is defined and can be called anywhere outside the function.

<!Doctype HTML><HTML><Head><title>Variable</title><MetaCharSet= "Utf-8" /></Head><Body>   <Script>    functionTest () {message= 'Hi';    alert (message); } test (); //Hialert (message);//Hi   </Script></Div></Body></HTML>

When defining multiple variables, you can separate them with commas

<!Doctype HTML><HTML><Head><title>Variable</title><MetaCharSet= "Utf-8" /></Head><Body>   <Script>    functionTest () {varmessage='Hi', found= false, Age= in;      alert (message);     alert (age); } test (); //Hi    </Script></Div></Body></HTML>

There are 5 basic types in ECMAScript: Undefined, null, Boolean, number, string, and a complex data type that object,object essentially consists of a set of unordered name-value pairs. ECMAScript does not support the creation of a mechanism for custom types, and all values are one of these 6 data types.

typeof operator

Since ECMAScript is loosely typed, there is a need for a means to detect the data type of a given variable. typeof is the operator responsible for providing this information. Using the typeof operator on a value may return one of the following strings

Undefined if this value is undefined

Boolean If this value is a Boolean value

String if this value is

Number if this value is a numeric type

Object If this value is a target or null

function if this value is

<!Doctype HTML><HTML><Head><title>Variable</title><MetaCharSet= "Utf-8" /></Head><Body>   <Script>         varmessage="some thing"; Alert (typeofmessage); Alert (typeof(message)); Alert (typeof  the); </Script></Div></Body></HTML>

The above example shows that the operands of the typeof operator can be either a variable or a numeric literal. Note that TypeOf is an operator instead of a function, so the parentheses in the example, although it can be used, are not required.

JavaScript variable data type typeof

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.