Data Structures and Methods

Numbers and Methods

You can create a number using the "new" keyword also.

When you create a number like that, you can see the Number prototype object, which contains all the properties and methods connected to the Number data type.

Numbers and Methods

.toFixed() // fix digits after decimal

const num = 1234567/6543 // 188.68515971267004
num.toFixed(2) // show 2 digits only after decimal, 188.68

////// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //////

.toPrecision() // number of significant digits

const num = 5.123456;

num.toPrecision() // '5.123456'
num.toPrecision(4) // '5.123'
num.toPrecision(1) // '5'

const num = 0.000123
num.toPrecision(5) // '0.00012300'
num.toPrecision(2) // '0.00012'

// note that exponential notation might be returned in some circumstances
(1234.5).toPrecision(2) // '1.2e+3' means 1.2 * 10^3 = 1200

Numbers and Methods

parseInt(str, base) //. converts string to number data-type
// base is opt, defaults to 10, between 2 to 36
.parseFloat(str) //. converts string to floating(decimal) number
.isFinite() 
// checks a number to be finite, 
// by checking number equal to +Infinity or -Infinity
.isInteger() // true if a value is an integer

isNaN() // true if a value is not a number
isNaN("I am a string")

Number.isSafeInteger() // if value is safe for JS
Number.isSafeInteger(Number.MAX_SAFE_INTEGER) // true

Number.MAX_VALUE // largest floating number
Number.MIN_VALUE // smallest floating number

Number.MAX_SAFE_INTEGER // largest integer
Number.MIN_SAFE_INTEGER // smallest integer

Number.NEGATIVE_INFINITY // returns -Infinity
Number.POSITIVE_INFINITY // returns +Infinity

Strings and Methods

let str = "A new string with words.";

str.toUpperCase()
str.toLowerCase()

str.split(separator|regex, limit) // limit is optional, no. of substrings to return
str.split(' ') // ['A', 'new', 'string', 'has', 'been', 'created']
str.split(' ', 3) // ['A', 'new', 'string']
/**
returns: array
changes the string in variable: FALSE
*/

str.substring(startIdx, endIdx) // endIdx is opt, takes substring from startIdx
// to endIDx, if endIdx is not given then takes from startIdx 
// all the way to the last letter.
str.substring(2, 12) // 'new string'
/**
returns: string
changes the string in variable: FALSE
*/

str.slice(startIdx, endIdx)
str.slice(-6, -1) // returns "words"

String is a primitive data-type and basically immutable, but they have an object representation as well.

Strings and Methods

Immutability of Strings

JavaScript strings are immutable. This means that once a string is created, modifying it is impossible.


​However, you can make a variable name point to a new value, but the previous value remains in memory,

The following sequence of events occurs when you try to mutate a string:

  • The existing value of the "name" variable is retrieved,

  • "Priyam" is appended to the existing value of "name",

  • The resultant value is then stored in a new block of memory,

  • The "name" variable now points to the newly created memory block,

  • Previously created memory space, still has a string 'Yash' stored in it, and is now available for garbage collection.

let name = "Yash"
name = "Priyam"
const str = "Yash"
str[1] // a
str[1] = "e"
str // "Yash"

Strings and Methods

str.endsWith(searchStr, endIdx) // endIdx is opt, if given searches upto endIdx
str.endsWith("words") // true
str.endsWith("A") // false
/**
returns: TRUE or FALSE
changes the string in variable: FALSE
*/

str.startsWith(searchStr, startIdx)// startIdx is opt
str.startsWith("A") // true
/**
returns: TRUE or FALSE
changes the string in variable: FALSE
*/


str.includes(searchStr, idx) // idx is opt, search starts from idx
str.includes("string") // TRUE
str.includes("STRING") // FALSE
/**
returns: TRUE or FALSE
changes the string in variable: FALSE
*/

Strings and Methods

str.trim() // removes whitespace from start and end of string
str.trimEnd()
str.trimStart()

str.at(-3) // gives character at given index (takes -ve index also)
str.charAt(2) // gives character at given index (takes +ve index only)


str.charCodeAt(index) 
// returns an integer between 0 and 65535 
// representing the UTF-16 code unit of the 
// character at the given index.
str.codePointAt(index)
str.fromCharCode()
str.fromCodePoint()

/*
 * Characters in a string are indexed from left to right. 
 * The index of the first character is 0, and 
 * last character str.length - 1.
 * Unicode code points range from 0 to 1114111 (0x10FFFF). 
 * In UTF-16, each string index is a code unit with value 0 – 65535. 
 * Higher code points are represented by a pair of 16-bit surrogate pseudo-characters. 
 * Therefore, codePointAt() returns a code point that may span two string indices.
 * */

Strings and Methods

str.search(regex)
/**
returns: idx of searched char, OR -1
changes the string in variable: FALSE
*/


str.match(regex)
/**
returns: an array of matches
changes the string in variable: FALSE
*/
str.matchAll()


str.replace(str | regex, replaceStr | function) 
// if str, then only 1st match is replaced

str.replaceAll(str | regex, replaceStr | function) 
// all the matches are replaced

W6_D1

By Yash Priyam

W6_D1

  • 134