In JavaScript, the trim() method is a string method used to remove whitespace characters from both the beginning and end of a string. Whitespace characters include spaces, tabs, and newlines. The trim() method does not modify the original string but returns a new string with the leading and trailing whitespace removed.
In TypeScript, string concatenation using the '+' operator allows you to combine multiple strings into a single string. When using the '+' operator, the strings on either side of it are joined together to form a new string. For example, if you have two variables 'firstName' and 'lastName', you can concatenate them to create a 'fullName' string by using the '+' operator and a space character in between. The resulting 'fullName' string will contain the combined value of the 'firstName' and 'lastName'.
In TypeScript, there are several ways to log variables and text together using console.log():
Concatenation:
You can concatenate the variable and text using the '+' operator, like this:
const name = "John";
console.log("Hello, " + name);
Template literals:
Use template literals (also known as template strings) enclosed in backticks (`) to embed variables directly within the string, like this:
const age = 30;
console.log(`I am ${age} years old.`);
//task6
let nameWithWhitespace: string = "\t John Doe \n";
// Print the name with whitespace
console.log("Name with whitespace: ");
console.log(nameWithWhitespace);
// Strip the whitespace from the name
let strippedName: string = nameWithWhitespace.trim();
// Print the name after stripping whitespace
console.log("\nStripped Name: ");
console.log(strippedName);
//task7
// Addition
let additionResult: number = 5 + 3;
console.log("Addition: " + additionResult);
// Subtraction
let subtractionResult: number = 10 - 2;
console.log("Subtraction: " + subtractionResult);
// Multiplication
let multiplicationResult: number = 4 * 2;
console.log("Multiplication: " + multiplicationResult);
// Division
let divisionResult: number = 16 / 2;
console.log("Division: " + divisionResult);
//task8
console.log(5 + 3);
console.log(10 - 2);
console.log(4 * 2);
console.log(16 / 2);
//task9
let favoriteNumber: number = 7;
console.log("My favorite number is: " + favoriteNumber);
//task 10
Adding Comments: Choose two of the programs you’ve written, and add at least one comment to each. If you don’t have anything specific to write because your programs are too simple at this point, just add your name and the current date at the top of each program file. Then write one sentence describing what the program does.
Multiple arguments:
Separate the variable and text with commas in the console.log() function to log them together:
const country = "USA";
console.log("I am from", country);
All three methods will log the variable and text together in the console output. Choose the one that suits your coding style and makes the code more readable.
In TypeScript, you can use different types of comments to document your code and provide explanations for yourself and other developers. The main types of comments in TypeScript are:
Single-line comments:
These comments start with // and continue until the end of the line. They are used to explain a single line of code or provide a brief description.
// This is a single-line comment
const age: number = 25; // Initializing the 'age' variable with value 25
Multi-line comments:
These comments start with /* and end with */. They can span multiple lines and are useful for documenting longer sections of code.
/*
This is a multi-line comment.
It can span multiple lines and is useful for explaining complex code.
*/
const firstName: string = "John";
const lastName: string = "Doe";