> For the complete documentation index, see [llms.txt](https://hemantajax-2.gitbook.io/typescript-step-by-step/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hemantajax-2.gitbook.io/typescript-step-by-step/types.md).

# Types

“locking” a variable definition to a particular kind of variable (number, string, etc.)

TypeScript has a small number of built-in types, including

* Number&#x20;
* String&#x20;
* Boolean&#x20;
* Array&#x20;
* Enum&#x20;
* Void

## Basic Type Example

Types can create clearer code and prevent errors

```javascript
function timesTwo( n : number ) : number { 
    return n * 2 
};

timesTwo(“6”); // compile-time error
var multiplied : string = timesTwo(5); // compile-time error
var num:number = timesTwo(5); // Correct
```

## Number

The number type can be set to any number

```javascript
var g : number;
g = 6;
g = 1.5;
g = 2 * 6;
g = 10e6;
g = Math.random();
g = "hello"; // compile-time error
```

## String

The string type can be any string – i.e., any sequence of Unicode characters

```javascript
var s : string;
s = "hello world";
s = "o" + "kay";
s = "fundamentals".slice(0,3);
s = 66; // compile-time error
```

## Boolean

Booleans have only four valid values – true, false, undefined and null

```javascript
var b : boolean;
b = true;
b = false;
b = undefined;
b = null;
b = 0; // compile-time error
b = "a"; // compile-time error
b = NaN; // compile-time error
```

## Array Types

Array types define both that an variable is an array and the kind elements it contains

```javascript
var numbers : number[] = [];
numbers[0] = 1;
numbers.push("two"); // compile-time error

var strings : string[] = [];
strings.push("hello");
strings[1] = 1337; // compile time error

var things : any[] = [];
things.push(1);
things.push("hello");
```

## Enum

giving more friendly names to sets of numeric values

```javascript
var colors: string[] = ['red', 'green', 'blue']; // colors[1] => green

// index 0 based (default)
enum colors {red, green, blue};
console.log(colors.green) // 1

// change index
enum colors { red=1, green=3, 'blue' };
console.log(colors.green); // 3

// get enum name using index
enum colors { red=1, green=3, 'blue' };
console.log(colors[3]); // green
```

## Any

* Can be anything&#x20;
* Similar to \* in other languages&#x20;
* To be avoided

```javascript
// Ex 1
var w : any;
w = "string";
w = 8;
w = false;
w = [];

// Ex 2
var list:any[] = [1, true, "free"];
```

## Void

* Absence of any type&#x20;
* Mostly used for function return signatures

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hemantajax-2.gitbook.io/typescript-step-by-step/types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
