JavaScript basic syntax-data type

Source: Internet
Author: User

Two JavaScript basic syntax

A JavaScript Data types

*****************************************************************

Different programming languages tend to have different data classifications, unlike object-oriented languages such as C and Java, where JavaScript variables do not require explicit data type declarations, which automatically determine the data type when the variable is first assigned to a value. This is one of the reasons why JavaScript is easy to use, as beginners tend to encounter problems with conversion of variable types, JavaScript simplifies the type of data, and conversions between data types can be performed automatically by rules. In fact, the data types except null and undefined in JavaScript are already encapsulated as objects.

*****************************************************************

* Data Type classification: basic type: Number,string,boolean

Special type: such as null,undefined

Combination type: such as Array,object

* Numeric type: no distinction between integral and floating-point values

All numbers are stored in 64-bit floating-point format , equivalent to the double format in the Java and C languages

* String Type

JavaScript is stored in string format, regardless of character or string .

String constants are a string of characters enclosed in single or double quotation marks, which can contain double quotation marks, or single quotes within double quotation marks, for example: "Can ' t", ' document.write ("Hello") '.

In client-side JavaScript programming, JavaScript code often contains HTML code strings, HTML code also often contains JavaScript code, like JavaScript, HTML also use single or double quotation marks to delimit the string, The usual practice is to use one quotation mark for HTML and another for JavaScript.

For example: <input type= "button" onclick= "alert (' Hello ')" >

If you need to use an apostrophe (') in a single-quote-delimited string, the single quotation mark in the string must use (\ ') and the same applies to double quotes.

Once a string variable is assigned, it automatically becomes the corresponding data type variable based on the type of the value, and the object's properties and methods can be called by object such as:

var aa= "javascript"; alert (aa.length); alert (Aa.substr (0,4));//returns 10 and "Java".

*null,undefined

Null means that the variable has no value or is not an object in the program

Undefined the value of the variable is not specified or the object property does not exist at all

Sample code:

<script language= "javascript" >//test null, empty string, Undefined, boolean-type relationship var aa=null; var bb= ""; var cc=Undefined;alert ("Determine if null and empty strings are equal"); alert (AA==BB);  Returns Falsealert ("Determine if null is equal to undefined"); alert (AA==CC);  Returns true, but does not have the same meaning as alert ("Determine if null and false are equal"); alert (aa==false);  Returns Falsealert ("Judging whether!null is equal to true"); alert (!aa==true);  Back to True</script>

* Type Conversion

JavaScript is loosely typed programming language

Variable does not need to specify a data type when declaring

Variable determines the data type only when it is assigned

The inclusion of different types of data in an expression forces class conversions to occur during the calculation

such as: number + string = = string; numeric + boolean ==0/1+ numeric calculation; string + Boolean = = String +true/false

* Coercion type conversion function

function parseint: Cast to an integer such as parseint ("6.12") =6

function parsefloat: Cast to floating-point numbers such as parsefloat ("6.12") =6.12

Function eval: Casts a string to an expression and returns the result such as eval ("=2,eval") ("1<2") =true

* Type query function typeof:

Querying numeric current type (String/number/boolean/object)

For example typeof ("Test" +3) = "string", typeof (NULL) = "Object"

JavaScript basic syntax-data type

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.