JavaScript: Methods Flashcards

1
Q

What is sort() method?
What is the syntax?

A

const comparisonOperators = [“Equal”, “Not Equal”, “Strict Equal”, “Strict Not Equal”, “Greater Than”, “Less Than”];
const arithmeticOperators = [”+”, “-“, “%”];
const logicalOperators = [“and”, “or”, “not”];
const myString = “Hello String”;
const numString = “1234”;

The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending.

// Sorts comparisonOperators array and returns the sorted array
comparisonOperators.sort();

// Logs sorted array
console.log(comparisonOperators);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the push() method?
What is the syntax?

A

const comparisonOperators = [“Equal”, “Not Equal”, “Strict Equal”, “Strict Not Equal”, “Greater Than”, “Less Than”];
const arithmeticOperators = [”+”, “-“, “%”];
const logicalOperators = [“and”, “or”, “not”];
const myString = “Hello String”;
const numString = “1234”;

// Adds elements to end of an array. Takes in at least one parameter
arithmeticOperators.push(“%”);

// Logs array with element “%” added to end
console.log(arithmeticOperators);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the slice() method?
What is the syntax?

A

const comparisonOperators = [“Equal”, “Not Equal”, “Strict Equal”, “Strict Not Equal”, “Greater Than”, “Less Than”];
const arithmeticOperators = [”+”, “-“, “%”];
const logicalOperators = [“and”, “or”, “not”];
const myString = “Hello String”;
const numString = “1234”;

// Returns selected elements as a new array.
const logicalOperatorsSliced = logicalOperators.slice(0,2);

// Logs new array
console.log(logicalOperatorsSliced);

// The original array is unchanged
console.log(logicalOperators);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the replace() method?
What is the syntax?

A

const comparisonOperators = [“Equal”, “Not Equal”, “Strict Equal”, “Strict Not Equal”, “Greater Than”, “Less Than”];
const arithmeticOperators = [”+”, “-“, “%”];
const logicalOperators = [“and”, “or”, “not”];
const myString = “Hello String”;
const numString = “1234”;

// Replaces “String” with “World” and returns new string
const myNewString = myString.replace(“String”, “World”);
console.log(myNewString);

// The original string is unchanged
console.log(myString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the parseInt method?

A

const comparisonOperators = [“Equal”, “Not Equal”, “Strict Equal”, “Strict Not Equal”, “Greater Than”, “Less Than”];
const arithmeticOperators = [”+”, “-“, “%”];
const logicalOperators = [“and”, “or”, “not”];
const myString = “Hello String”;
const numString = “1234”;

// parseInt is a built in method that will convert a string into an integer:
const convertedNum = parseInt(numString);
// logs number
console.log(typeof convertedNum);
// the original string is unchanged
console.log(numString)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly