Typescript Lecture 2 (Errors & Variable )

Опубликовано: 20 Май 2025
на канале: Quoteonomics
237
9

Syntax Error:
In computer programming, a syntax error is a type of error that occurs when the syntax (grammar and structure) of a program's code does not conform to the rules of the programming language. These errors prevent the program from being compiled or executed successfully.
lett message = "Hello World";//syntax error
console.log(message);

Type Error:
A type error, in the context of computer programming, refers to an error that occurs when a program attempts to use a value or perform an operation on a data type that is not compatible with the expected data type.
let message = "Hello World";
console.loger(message); //type error

Assignability Error
An assignability error, in the context of computer programming, occurs when there is a mismatch in the types of variables being assigned or assigned to. This error happens when the data types of variables involved in an assignment operation are not compatible, and the language's type system prevents the assignment from being performed.
let message: string = "Hello World";
message = 6; // assigning numeric value to string variable
console.log(message);

In TypeScript, variable types refer to the specific data types that can be assigned to a variable. TypeScript is a statically typed language, which means that variables are associated with a particular data type at the time of declaration and cannot change their type during runtime. Here are some commonly used variable types in TypeScript:

number: Represents numeric values, both integers and floating-point numbers.

string: Represents textual data, enclosed within single or double quotes.

boolean: Represents boolean values, either true or false.

let and const are two relatively new concepts for variable declarations in JavaScript. As we mentioned earlier, let is similar to var in some respects, but allows users to avoid some of the common “gotchas” that users run into in JavaScript.

const is an augmentation of let in that it prevents re-assignment to a variable.

With TypeScript being an extension of JavaScript, the language naturally supports let and const. Here we’ll elaborate more on these new declarations and why they’re preferable to var.

Regular expressions, often called regex or regexp, are powerful tools used for pattern matching and manipulating text in strings. They allow you to search, find, and replace specific patterns in a given text.

Here's a breakdown of the regex pattern you provided: /\b\w/g

/: The regex pattern starts and ends with forward slashes (/). These are used to delimit the regular expression.

\b: This is a word boundary metacharacter. It matches the position between a word character (a-z, A-Z, 0-9, or underscore) and a non-word character (anything other than a word character). It's often used to match whole words.

\w: This is a word character metacharacter. It matches any word character, which includes uppercase and lowercase letters, digits, and underscores.

g: This is a global flag. When used with the regex pattern, it enables global searching, which means it will find all occurrences of the pattern in the entire input string, rather than stopping after the first match.

Combining all these elements, the regex pattern /\b\w/g will match all individual word characters in a string, effectively breaking the string down into its constituent letters (ignoring non-word characters like spaces and punctuation marks). For example, given the input string "Hello, World!", the regex will match the letters "H", "e", "l", "l", "o", "W", "o", "r", "l", "d", and the exclamation mark will be ignored.

\d: Matches any digit (0-9).
\D: Matches any non-digit character.
\s: Matches any whitespace character (spaces, tabs, line breaks).
\S: Matches any non-whitespace character.
\w+: Matches one or more word characters (letters, digits, underscores).
\W: Matches any non-word character (anything other than letters, digits, underscores).
.: Matches any single character except for a newline.
^: Matches the start of a string.
$: Matches the end of a string.
[abc]: Matches any character within the square brackets (a, b, or c).
\b: Matches the whole word "word" as a separate word boundary.