print("Hello, world!")
console.log("Hello, world!");
var myVariable = 42
myVariable = 50
let myConstant = 42
var myVariable = 42;
myVariable = 50;
const myConstant = 42; // Constant
/* NOTE::
* let and var are very similar when used like this outside
* a function block
*/
let name = 'TypeScript'; // globally scoped
var age = 5; // globally scoped
/*
* Also, They are identical when used like this in
* a function block.
*/
function ingWithinEstablishedParameters() {
let name = 'TypeScript'; //function block scoped
var age = 5; //function block scoped
}
/*
* But the difference is when you used it insied a block.
* In example below; let is only visible in the for() loop
* and var is visible to the whole function.
*/
function myFunction() {
//x is *not* visible out here
for( let x = 0; x < 5; x++ ) {
//x is only visible in here and in the for() parentheses
//and there is a separate tuce variable for each
// iteration of the loop
}
//x is *not* visible out here
}
function myFunction() {
//x *is* visible out here
for( var x = 0; x < 5; x++ ) {
//x is visible to the whole function
}
//x *is* visible out here
}
let explicitDouble: Double = 70
let explicitDouble: number = 70;
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
let label = "The width is ";
let width = 94;
let widthLabel = label + String(width);
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
"pieces of fruit."
let apples = 3;
let oranges = 5;
let fruitSummary = `I have ${apples + oranges} ` +
"pieces of fruit.";
let names = ["Anna", "Alex", "Brian", "Jack"]
names.enumerated().forEach { (offset, element) in
print("Person \(offset + 1) is called \(element)")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
let names = ["Anna", "Alex", "Brian", "Jack"];
names.forEach((value: string, index: number) => {
console.log("Person " + index + " is called " + value);
});
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
for (let index = 1; index <= 5;index ++){
console.log(index + " times 5 is " + (index * 5));
}
// OR //
Array.from({ length: 5 }, (value, key) => {
console.log((key+1) + " times 5 is " + ((key+1) * 5));
});
var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var shoppingList: string[] = ["catfish", "water",
"tulips", "blue paint"];
shoppingList[1] = "bottle of water";
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
var occupations:{ [key: string]: string; } = {}
occupations["Malcolm"] = "Captain";
occupations["Kaylee"] = "Mechanic";
occupations["Jayne"] = "Public Relations";
let emptyArray = [String]()
let emptyArray: string[] = []
func greet(_ name: String,_ day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
function greet(name: string, day: string): string {
return "Hello " + name + ", today is " + day + ".";
}
greet("Bob", "Tuesday");
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
function getGasPrices(): [number, number, number] {
return [3.59, 3.69, 3.79]
}
func sumOf(_ numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf(42, 597, 12)
function sumOf(...numbers: number[]): number {
var sum = 0
for (let number of numbers) {
sum += number;
}
return sum
}
sumOf(42, 597, 12);
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
let increment = makeIncrementer()
increment(7)
let makeIncrementer: (value: number) => number =
function (value: number): number {
return value + 1;
};
let increment = makeIncrementer;
console.log(increment(7));
let numbers = [20, 19, 7, 12]
numbers.map { 3 * $0 }
let numbers = [20, 19, 7, 12];
numbers.map(value => console.log(value * 3));
var mutableArray = [1, 5, 3, 12, 2]
mutableArray.sort()
var mutableArray = [1, 5, 3, 12, 2];
mutableArray.sort((n1,n2) => n1 - n2);
func area(width: Int, height: Int) -> Int {
return width * height
}
area(width: 2, height: 3)
function area({ width, height }: { width: number, height: number }): number {
return width * height
}
area({ width: 2, height: 3 })
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Shape {
private numberOfSides = 0;
simpleDescription(): string {
return `A shape with ${this.numberOfSides} sides.`;
}
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
var shape = new Shape();
shape.numberOfSides = 7;
var shapeDescription = shape.simpleDescription();
class NamedShape {
var numberOfSides: Int = 0
let name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
self.numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length " +
sideLength + "."
}
}
let test = Square(sideLength: 5.2, name: "square")
test.area()
test.simpleDescription()
class NamedShape {
numberOfSides: number = 0;
name: string;
constructor({name}: {name: string}) {
this.name = name;
}
simpleDescription(): string {
return `A shape with ${this.numberOfSides} sides.`;
}
}
class Square extends NamedShape {
sideLength: number;
constructor({sideLength, name}: {sideLength: number, name: string}) {
super({name});
this.sideLength = sideLength;
this.numberOfSides = 4;
}
area(): number {
return this.sideLength * this.sideLength
}
simpleDescription(): string {
return `A square with sides of length ${this.sideLength}.`
}
}
let test = new Square({sideLenght: 5.2, name: "square"});
test.area();
test.simpleDescription();
var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
}
}
var movieCount = 0
var songCount = 0
for (let item of library) {
if (item instanceof Movie){
++movieCount;
} else if(item instanceof Song){
++songCount;
}
}
let nb = 42
switch nb {
case 0...7, 8, 9: print("single digit")
case 10: print("double digits")
case 11...99: print("double digits")
case 100...999: print("triple digits")
default: print("four or more digits")
}
let nb = 42
switch (true) {
case nb >= 0 && nb <= 9:
console.log("single digit");
case nb === 10:
console.log("double digits");
case nb >= 11 && nb <= 99:
console.log("double digits");
case nb >= 100 && nb <= 999:
console.log("triple digits");
default:
console.log("four or more digits");
}
for current in someObjects {
if let movie = current as? Movie {
print("Movie: '\(movie.name)', " +
"dir. \(movie.director)")
}
}
for (let current of someObjects) {
if(current instanceof Movie) {
console.log("Movie: " + current.name + ", " +
"dir. " + current.director);
}
}
protocol Nameable {
func name() -> String
}
func f<T: Nameable>(x: T) {
print("Name is " + x.name())
}
interface Nameable {
name(): string;
}
printName(x: Nameable) {
console.log("Name is " + x.name());
}
extension Double {
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
interface Number {
mm(): number;
ft(): number;
}
Number.prototype.mm = function (): number {
return this / 1000;
}
Number.prototype.ft = function (): number {
return this / 3.28084;
}
let oneInch = 25.4.mm();
console.log("One inch is " + oneInch + " meters");
// prints "One inch is 0.0254 meters"
let threeFeet = 3.0.ft();
console.log("Three feet is " + threeFeet + " meters");
// prints "Three feet is 0.914399970739201 meters"