How to Format Currency to 2 Decimals in TypeScript

Format currency to 2 decimals in TypeScript: Command Prompt output of Intl.NumberFormat with USD

You can format currency to 2 decimals in TypeScript with Intl.NumberFormat: pass style: ‘currency’ and currency: ‘USD’, and you get a string like $1,234.50 with the symbol, commas and two decimals: Every output on this page came from a real run with TypeScript 7.0.2 and Node.js v22.22.2, strict mode on. Format currency to 2 decimals … Read more >>

How to Convert Number to Boolean in TypeScript

Convert number to boolean in TypeScript: Command Prompt output of Boolean(), !! and n !== 0 for 0, -0, NaN and Infinity

The quickest way to convert number to boolean in TypeScript is Boolean(n) or !!n. Both return false for 0, -0 and NaN, and true for every other number, including negatives and Infinity: I ran each snippet below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the console output you see is real. We’ll … Read more >>

The require Keyword in TypeScript (import = require, TS2591, ESM)

require in TypeScript: Command Prompt output of a CommonJS file requiring an ES module in Node.js 22

The require keyword in TypeScript is really Node.js’s CommonJS require() function: it loads a module synchronously and returns its exports. In a .ts file it needs Node’s types, and it returns any, so prefer import x = require() or plain import: Every example here ran on TypeScript 7.0.2 and Node.js v22.22.2, compiled in strict mode. … Read more >>

How to Get String Between 2 Characters in TypeScript

Get string between 2 characters in TypeScript: Command Prompt output of matchAll pulling every value between brackets

You can get string between 2 characters in TypeScript with indexOf() and slice(): find the start character, search for the end character from just after it, and slice the text in between. Here’s the core of it: Nothing below is typed from memory: I saved each example as a .ts file, compiled it in strict … Read more >>

How to Split a String by Dot in TypeScript (With Examples)

Split string by dot in TypeScript: Command Prompt output of splitting file names into name and extension

To split a string by dot in TypeScript, call split(“.”). It returns a string[] of the pieces between each period. Pass the plain string “.”, not /./, because an unescaped regex dot matches every character: I ran all of this code through TypeScript 7.0.2 and Node.js v22.22.2 with strict mode on, and the outputs below … Read more >>

How to Get the Decimal Part of a Number in TypeScript

TypeScript decimal part of a number: Command Prompt output comparing % 1, trunc and floor for negative numbers

You can get the decimal part of a number in TypeScript with the remainder operator: x % 1 returns everything after the decimal point as a number. For 24.75 that’s 0.75, and an integer gives 0: My test machine runs TypeScript 7.0.2 and Node.js v22.22.2; I built each file with strict checks on and copied … Read more >>

How to Find a Key by Value in a TypeScript Dictionary

Find key by value in a TypeScript dictionary: Command Prompt output of Object.keys with find

To find a key by value in a TypeScript dictionary, get the keys with Object.keys(), type them as keyof, and call find() to return the first key whose value matches. If nothing matches, you get undefined: Nothing below is pseudo-code: each lookup went through tsc –strict and then Node, using TypeScript 7.0.2 and Node.js v22.22.2, … Read more >>

How to Split a String by Regex in TypeScript

Split string by regex in TypeScript: Command Prompt output of camelCase and sentence splits with lookahead and lookbehind

You split a string by regex in TypeScript by passing a RegExp to split() instead of a string. Every match becomes a cut point, and you get back a string[]: Each snippet here was compiled in strict mode with TypeScript 7.0.2 and Node.js v22.22.2. I pasted the console output straight from those runs. How split() … Read more >>

ReferenceError: fetch is not defined in TypeScript (Fixed)

ReferenceError fetch is not defined in TypeScript: Command Prompt output from Node.js with and without global fetch

ReferenceError: fetch is not defined in TypeScript means your compiled JavaScript is running somewhere that has no global fetch, usually Node.js older than 18 or a Jest jsdom test. Upgrade Node, or load node-fetch first: Every example below ran on TypeScript 7.0.2 and Node.js v22.22.2 with strict mode, and I copied the output straight from … Read more >>