Methods

1- String Methods (w3schools)

const myString = "javascript is my name";

console.log(myString.length);        //returns length

console.log(myString.indexOf("is"));       //returns position of "is"
console.log(myString.lastIndexOf("a"));       //returns position of "a", -1 if not found

console.log(myString.slice(2,5));       //returns substring from index 2 to 5
console.log(myString.substring(3,6));       //returns substring from index 2 to 5, can't accept neg arguments
console.log(myString.substr(3,4));       //returns substring from index 3 of length 4

console.log(myString.replace("javascript","js"))       //replace "javascript" with "js"

console.log(myString.toUpperCase())       //change to upper case
console.log(myString.toLowerCase())       //change to lower case

console.log(myString.concat(", you know"))       //stick two strings

console.log(myString.trim())       //removes extra space from string end

console.log(myString.charAt(0))       //return character at index 0
console.log(myString.charCodeAt(0))       //return utf code of character at index 0

There are more methods. Read them here - MDN 

 

 

2- Number Methods(w3schools)

const yearOfMyBirth = 1995;

console.log();


console.log(yearOfMyBirth.toString());             //converts number to string

console.log(yearOfMyBirth.toExponential(2));     //return number in exponential notation

console.log(yearOfMyBirth.toFixed(2));        //returns a string with 2 decimal places
console.log(yearOfMyBirth.toPrecision(2));          //returns a string with a length of 2

console.log((1996 + 5).valueOf());   // returns 2000 from expression 100 + 23

console.log(Number("1995"));  // returns number

console.log(parseInt("10.33"));     // returns a number after converting from float

There are more methods. Read them here - MDN 

 

3- Array Methods(w3schools)

const myName = ['My', 'Name', 'is' , 'javascript'];

console.log(myName.join('-'));                  //returns a string after joining all elements of array

console.log(myName.pop());                  //removes rightmost element of an array
console.log(myName.push('or js'));                  //push one new element from the right

console.log(myName.shift());                  //removes one element from the left or you can say first
console.log(myName.unshift('Hi'));                  //adds one element from the left or you can say first

console.log(myName.length);                  //returns array length

console.log(myName[2]);                  //returns element at index 2

console.log(myName.sort());                  //sort the array

Many string and array methods are the same and work in the same manner. 
Not to mention, there are more methods. Read them here - MDN 


Discussion

Be the first one to comment!