Understanding the Different Types in Typescript (e.g. Number, String, Boolean)
Welcome to the “Understanding the Different Types in TypeScript” section of our course “Learn TypeScript”! In this article, we will cover the various types available in TypeScript, including numbers, strings, and booleans. By the end of this article, you should have a good understanding of these types and be able to use them effectively in your TypeScript programs.
Numbers
TypeScript has two types for representing numbers: “number” and “bigint”. The “number” type represents a double-precision floating-point number, which is a number with a decimal point. The “bigint” type represents a large integer value. You can specify a number literal as a “number” by adding a decimal point or an exponent, or you can specify it as a “bigint” by adding the “n” suffix. For example:
let x: number = 10;
let y: bigint = 10n;
In this example, the variable “x” is of type “number” and the variable “y” is of type “bigint”.
TypeScript also supports the standard arithmetic operators for numbers, such as “+”, “-“, “*”, and “/”. For example:
let x: number = 10;
let y: number = 20;
let z: number = x + y; // 30
In this example, the variables “x” and “y” are both of type “number” and the result of their addition is also of type “number”.
Strings
TypeScript has a single type for representing strings, called “string”. You can specify a string literal by enclosing it in single or double quotes. For example:
let x: string = 'hello';
let y: string = "world";
In this example, the variables “x” and “y” are both of type “string”.
TypeScript also supports the standard string operations, such as concatenation, interpolation, and slicing. For example:
let x: string = 'hello';
let y: string = 'world';
let z: string = x + ' ' + y; // 'hello world'
let a: number = 10;
let b: string = `the value of a is ${a}`; // 'the value of a is 10'
let c: string = 'hello world';
let d: string = c.slice(0, 5); // 'hello'
In this example, the variables “z” and “b” are both of type “string” and are created using concatenation and interpolation, respectively. The variable “d” is also of type “string” and is created by slicing the string “c”.
Booleans
TypeScript has a single type for representing boolean values, called “boolean”. You can specify a boolean literal by using the “true” or “false” keywords. For example:
let x: boolean = true;
let y: boolean = false;
In this example, the variable “x” is of type “boolean” and has a value of “true”, while the variable “y” is also of type “boolean” and has a value of “false”.
TypeScript also supports the standard boolean operations, such as “and” (&&), “or” (||), and “not” (!). For example:
let x: boolean = true;
let y: boolean = false;
let a: boolean = x && y; // false
let b: boolean = x || y; // true
let c: boolean = !x; // false
In this example, the variables “a”, “b”, and “c” are all of type “boolean” and are created using the “and”, “or”, and “not” operations, respectively.
Other Types
In addition to numbers, strings, and booleans, TypeScript also has several other types that you may encounter in your code. These include:
- “null” and “undefined”: These types represent the absence of a value. The “null” type represents the intentional absence of a value, while the “undefined” type represents the default value for variables that have not been assigned a value. You can specify a null or undefined literal by using the “null” or “undefined” keywords.
- “void”: This type represents the absence of a return value for a function. You can specify a void type by using the “void” keyword.
- “never”: This type represents a value that will never occur. You can specify a never type by using the “never” keyword.
Conclusion
In conclusion, this article has covered the basic types in TypeScript, including numbers, strings, and booleans. You should now have a good understanding of these types and how to use them in your TypeScript programs. In future sections of our course, we will cover more advanced types, such as union types and type aliases, and how to use them to build larger and more complex applications.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
What are the two types for representing numbers in TypeScript?
The two types for representing numbers in TypeScript are “number” and “bigint”. The “number” type represents a double-precision floating-point number, while the “bigint” type represents a large integer value. You can specify a number literal as a “number” by adding a decimal point or an exponent, or you can specify it as a “bigint” by adding the “n” suffix.
How do you specify a string literal in TypeScript?
To specify a string literal in TypeScript, you can enclose it in single or double quotes. For example:
let x: string = 'hello';
let y: string = "world";
What are the standard string operations in TypeScript?
The standard string operations in TypeScript include concatenation, interpolation, and slicing. Concatenation allows you to combine multiple strings into a single string using the “+” operator. Interpolation allows you to insert the value of a variable into a string using the “`” and “${}” syntax. Slicing allows you to extract a portion of a string using the “slice” method.
How do you specify a boolean literal in TypeScript?
To specify a boolean literal in TypeScript, you can use the “true” or “false” keywords. For example:
let x: boolean = true;
let y: boolean = false;
What are the other types in TypeScript besides numbers, strings, and booleans?
The other types in TypeScript besides numbers, strings, and booleans include “null”, “undefined”, “void”, and “never”. The “null” and “undefined” types represent the absence of a value, with “null” representing the intentional absence of a value and “undefined” representing the default value for variables that have not been assigned a value. The “void” type represents the absence of a return value for a function. The “never” type represents a value that will never occur.