Previous 6 Next

TypeScript

We are using TypeScript and below follows code examples that show most of the language features used in this book.

Variables

let x = 10
const y = 10
let yes: boolean = true
const no: boolean = false

Strings

const stringDoubleQuotes = "hello world"
const stringSingleQuotes = 'Hello world'
const stringBackticks = `Hello world`
const multilineString = `
Hello
world
`
const x = 55
const templateString = `x is ${x}`

Functions

function add(a: number, b: string): number {
  return a + b
}
const add2 = (a: number, b: number) => { return a + b }
const add3 = (a: number, b: number) => a + b

add(1, 1)

Classes

class Person {
  constructor(
    public name: string
  )
  sayHello() {
    console.log("Hello")
  }
}

const person = new Person("Jane")
person.sayHello()

Interfaces

interface Cool {
  beCool(): void
}

class B implements Cool {
  beCool() {}
}

const b: Cool = new B

Iteration

for (let i = 0; i < 10; i++) {
    
}

let i = 0
while (i < 10) {
  i++
}

Arrays and array traversal

const digits: number[] = [1, 2, 3, 4]
const words = ["hello", "world"]

for (let i = 0; i < words.length; i++) {
  const word = words[i]
}

for (const word of words) {
}

words.forEach(word => {
})

If, switch and conditional operator

const x = 10

if (x == 1) {

} else if (x == 2) {

} else {

}

switch (x) {
  case 1:
    break;
  case 2: 
    break;
  default:
    break;
}

const y = 10 < 5 ? "is less" : "is greater or equal"

That is it

If you are familiar with the code in the examples above you should be able to understand most of the code in this book.

Links

Previous 6 Next