Logo
Node.js
Node.jsJavaScript and TypeScript Basics

JavaScript and TypeScript Basics

Before diving into the intricacies of building RESTful services with Node.js, it's essential to have a solid understanding of JavaScript and TypeScript fundamentals. This section serves as a quick refresher to get you up to speed with the languages you'll be using throughout this training.

JavaScript Basics

Variables and Data Types

JavaScript has various data types like Number, String, Boolean, Object, and Array. Variables can be declared using var, let, or const.

let name = "John";
const age = 30;

Functions

Functions are reusable blocks of code. They can be declared using function declarations or function expressions.

function greet() {
  console.log("Hello, World!");
}
 
const greet = function() {
  console.log("Hello, World!");
};

Control Structures

JavaScript offers control structures like if-else, switch, for, and while loops for conditional and iterative logic.

if (age > 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

Asynchronous Programming

JavaScript handles asynchronous operations using callbacks, promises, and async-await.

async function fetchData() {
  const data = await fetch("https://api.example.com/data");
  return data.json();
}

TypeScript Basics

TypeScript is a superset of JavaScript that adds static types to the language. It compiles down to plain JavaScript and offers better tooling and error checking.

Static Typing

You can specify types for variables, function parameters, and return values.

let name: string = "John";
function greet(name: string): void {
  console.log(`Hello, ${name}`);
}

Interfaces and Classes

TypeScript supports object-oriented programming with classes and interfaces.

interface Person {
  name: string;
  age: number;
}
 
class Employee implements Person {
  constructor(public name: string, public age: number) {}
}

Generics

Generics allow you to write reusable and type-safe code.

function getData<T>(data: T): T {
  return data;
}

Summary

Understanding the basics of JavaScript and TypeScript is crucial for effective Node.js development. Whether you're dealing with variables, functions, or asynchronous operations in JavaScript, or exploring static typing and object-oriented features in TypeScript, these foundational concepts will serve as the building blocks for your RESTful services.

Book a conversation with us for personalize training today!

Was this helpful?
Logo