Homework

RMAG news
#- Complex data types
#- Math object
#- String
#- Number
#- TypeConversion
#- if else

– Complex data types

🎯 Object Data Type
The object is a complex data type in JavaScript that allows you to store and manipulate collections of data. An object can be created using two methods: object literals and object constructors.

Object Literals
Object literals are the simplest way to create objects in JavaScript. An object literal is a comma-separated list of name-value pairs wrapped in curly braces. The name of each property is a string or a number, followed by a colon and the property value.

const car = {
make: “Toyota”,
model: “Camry”,
year: 2021,
};

Object Constructors
An object constructor is a function that is used to create an object. The constructor function is defined with the function keyword, and the properties of the object are defined using the this keyword.

function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}

const myCar = new Car(“Toyota”, “Camry”, 2021);

🎯 Array Data Type
Arrays are another complex data type in JavaScript that allows you to store and manipulate collections of data. An array can be created using two methods: array literals and array constructors.

Array Literals
Array literals are the simplest way to create arrays in JavaScript. An array literal is a comma-separated list of values wrapped in square brackets.

const colors = [“red”, “green”, “blue”];

Array Constructors
An array constructor is a function that is used to create an array. The constructor function is defined with the Array keyword, and the elements of the array are defined as arguments.

const numbers = new Array(1, 2, 3, 4, 5);

Math Object

Unlike most global objects, Math is not a constructor. You cannot use it with the new operator or invoke the Math object as a function. All properties and methods of Math are static.

Math.abs()
Returns the absolute value of x.

Math.ceil()
Returns the smallest integer greater than or equal to x.

Math.floor()
Returns the largest integer less than or equal to x.

Math.max()
Returns the largest of zero or more numbers.

Math.min()
Returns the smallest of zero or more numbers.

Math.pow()
Returns base x to the exponent power y (that is, xy).

Math.random()
Returns a pseudo-random number between 0 and 1.

Math.round()
Returns the value of the number x rounded to the nearest integer.

Math.sqrt()
Returns the positive square root of x.

Math.trunc()
Returns the integer portion of x, removing any fractional digits.

String

Name Description

charAt() Returns the character at a specified index (position)

charCodeAt() Returns the Unicode of the character at a specified index

concat() Returns two or more joined strings

constructor Returns the string’s constructor function

endsWith() Returns if a string ends with a specified value

fromCharCode() Returns Unicode values as characters

includes() Returns if a string contains a specified value

indexOf() Returns the index (position) of the first occurrence of a value in a string

lastIndexOf() Returns the index (position) of the last occurrence of a value in a string

length Returns the length of a string

localeCompare() Compares two strings in the current locale

match() Searches a string for a value, or a regular expression, and returns the matches

prototype Allows you to add properties and methods to an object

repeat() Returns a new string with a number of copies of a string

replace() Searches a string for a pattern, and returns a string where the first match is replaced

replaceAll() Searches a string for a pattern and returns a new string where all matches are replaced

search() Searches a string for a value, or regular expression, and returns the index (position) of the match

slice() Extracts a part of a string and returns a new string

split() Splits a string into an array of substrings

startsWith() Checks whether a string begins with specified
characters

substr() Extracts a number of characters from a string, from a start index (position)

substring() Extracts characters from a string, between two specified indices (positions)

toLocaleLowerCase() Returns a string converted to lowercase letters, using the host’s locale

toLocaleUpperCase() Returns a string converted to uppercase letters, using the host’s locale

toLowerCase() Returns a string converted to lowercase letters

toString() Returns a string or a string object as a string

toUpperCase() Returns a string converted to uppercase letters

trim() Returns a string with removed whitespaces

trimEnd() Returns a string with removed whitespaces from the end

trimStart() Returns a string with removed whitespaces from the start

valueOf() Returns the primitive value of a string or a string object

Number

let x = 3.14; // A number with decimals
let y = 3; // A number without decimals

Numeric Strings
JavaScript strings can have numeric content:

let x = 100; // x is a number

let y = “100”; // y is a string

NaN – Not a Number

let x = 100 / “Apple”;

Automatic Type Conversion

5 + null // returns 5 because null is converted to 0
“5” + null // returns “5null” because null is converted to “null”
“5” + 2 // returns “52” because 2 is converted to “2”
“5” – 2 // returns 3 because “5” is converted to 5
“5” * “2” // returns 10 because “5” and “2” are converted to 5 and 2

if else

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
if (hour < 18) {
greeting = “Good day”;
} else {
greeting = “Good evening”;
}
Please follow and like us:
Pin Share