TypeScript syntax (1) -- Basic Data Type

Source: Internet
Author: User

TypeScript syntax (1) -- Basic Data Type
1. Boolean)

var isDone: boolean = true;
2. Number)
var height: number = 6;
Iii. String type)

You can use double quotation marks or single quotation marks.

var name: string = "bob";name = 'smith';
4. Array)

Two methods to declare an array:

First:

var list:number[] = [1,2,3];

Note:numberIndicates the Data Type of elements in the array,[]Declares an array.

Second:

var list:Array
  
    = [1,2,3];
  

Note:ArrayDeclares an array, Indicates the Data Type of elements in the array.

5. Enumeration type (Enum)
enum Color {Red, Green, Blue};var c: Color = Color.Green;

By default, the index starting with a member of the enumeration type is0(RedThe index is0), You can manually change the start index of the member in the enumeration type, for example:

enum Color {Red = 1, Green, Blue};var c: Color = Color.Green;

RedThe index is1You can also specify an index for each member, for example

Enum Color {Red = 1, Green = 2, Blue = 4}; var c: Color = Color. Green; // note c: there is no space between each character of Color.

Value Through Index

enum Color {Red = 1, Green, Blue};var colorName:String = Color[2];alert(colorName);
6. Any type (Any)

A variable that describes unknown types, or the variable type changes dynamically. It must be declaredanyType to make the variable pass the variable type check during compilation.

var notSure: any = 4;notSure = "maybe a string instead";notSure = false;

Arrays containing different types of elements:

var list:any[] = [1, true, "free"];list[1] = 100;
7. No type (void)

The following function does not return any value

function warnUser(): void {    alert("This is my warning message");}

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.