> For the complete documentation index, see [llms.txt](https://hemantajax-2.gitbook.io/javascript-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/javascript-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

Collection of unique strings

```javascript
enum OrderStatus {complete,pending,declined};
var n : OrderStatus;
n = OrderStatus.complete;
n = OrderStatus.unfinished; // compile-time error
n = "on the way"; // compile-time error
```

## Any

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

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

## Void

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

```javascript
function nothing () : void  {
    var g = "I don't return anything.";
}
```
