Check if a TypeScript Array Contains a Specific Value

Check if a TypeScript array contains a specific value: Command Prompt output of includes() on an array of cities

To check if a TypeScript array contains a specific value, call includes(): cities.includes(“Denver”) returns true or false. That works for strings, numbers and booleans. For an array of objects it will not, because includes() compares by reference, so you test a property with some() instead: orders.some(o => o.id === “ORD-1001”). This guide covers both, plus … Read more >>

How to Get Distinct Values from an Array in TypeScript

TypeScript distinct values: Command Prompt output of new Set removing repeated state codes from an array

To get distinct values from an array in TypeScript, pass the array to a Set and spread it back: […new Set(arr)]. A Set keeps each value once, in first-seen order, and TypeScript keeps the element type, so a string[] stays a string[]: I compiled these snippets with TypeScript 7.0.2 running on Node.js v22.22.2. One of … Read more >>

Fix Error TS2550: Property ‘includes’ Does Not Exist on Type ‘string’

TypeScript error TS2550 property includes does not exist on type string in the Command Prompt

Error TS2550 “Property ‘includes’ does not exist on type ‘string’” means your lib setting is older than ES2015, so TypeScript doesn’t know String.prototype.includes exists. Fix it in tsconfig.json by setting lib (or target) to es2015 or later, such as: These errors come straight from TypeScript 7.0.2 and Node.js v22.22.2. A few of the messages changed … Read more >>

How to Find Prime Numbers in a Range Using Python

Find prime numbers in a range using Python: Command Prompt output listing the prime years from 2000 to 2030

The simplest way to find prime numbers in a range using Python is a loop over every number from low to high that keeps the ones that have no divisor between 2 and their square root. One list comprehension does it: I ran every listing below with Python 3.12.5 and SymPy 1.14.0 and pasted the … Read more >>

How to Check if a Word is in a String in Python

Python check if word in string: Command Prompt output of a regex word-boundary check on a support ticket

To check if a word is in a string in Python, use the in operator for a quick test, or re.search() with \b word boundaries when it has to be the whole word, not part of a longer one: I ran each script in Command Prompt on Python 3.12.5, with customer reviews, a support ticket … Read more >>

How to Find the Maximum of Three Numbers in Python

Maximum of three numbers in Python: Command Prompt output comparing a strict if-elif chain with a tie-safe version

To find the maximum of three numbers in Python, call the built-in max() with all three values: max(a, b, c) returns the largest one. Wrap it in a function if you need it more than once: Every program below was run on Python 3.12.5 in Command Prompt, and the outputs shown are copied from those … Read more >>

How to Convert a String to CSV in Python

Python convert string to CSV: Command Prompt output of csv.reader parsing a CSV string with quoted commas

To convert string to CSV in Python, write the text to a file opened with newline=”” when it’s already comma-separated, or parse it into rows with csv.reader(io.StringIO(text)) and save those rows with csv.writer: I ran each script below on Python 3.12.5 and pandas 3.0.6, feeding it small US sales and order samples you can copy. … Read more >>

How to Filter Arrays in TypeScript

Filter arrays in TypeScript: Command Prompt output of filter() selecting numbers above a limit and city names beginning with A

To filter an array in TypeScript, call filter() with a function that returns true for the elements you want to keep: temperatures.filter(temp => temp > 80). It returns a new array and never changes the original, so the result of filtering a number[] is a number[]. The one place TypeScript surprises people is removing undefined … Read more >>

How to Sort Arrays in TypeScript

Sort arrays in TypeScript: Command Prompt output of sort() with and without a compare function on numbers and strings

To sort an array in TypeScript, call sort() with a compare function: numbers.sort((a, b) => a – b) for numbers and names.sort((a, b) => a.localeCompare(b)) for text. Swap a and b for descending order. Without a compare function sort() turns every element into a string first, which is why [100, 25, 9, 1000, 3] comes … Read more >>

How to Convert a String to Float in Python (Commas, Errors)

Convert a string to float in Python: Command Prompt output showing float() results and ValueError could not convert string to float messages

To convert a string to a float in Python, pass it to float(): float(“19.99”) returns 19.99. If the string contains thousands separators, remove them first (float(“1,234.5”.replace(“,”, “”))), and wrap the call in try/except ValueError when the text may not be a number. This guide shows how to convert string to float in Python in every … Read more >>