Day 34 - 9/10

Plan: Continue learning JavaScript

Progress:

Data types:

  • Js types are dynamic, which means the same var can have different data types

  • when add number and string, the number will be treated as string; caculation still be done if there is no string before that

  • array: const name = [a, b, c];

  • objects: const name = {a, b, c}; : works like struct

  • undefined: a var without value hold value undefined and type undefined; string without value hold value "" and type undefined

Function

  • function name(parameter1, parameter2, parameter3) { // code to be executed }

  • Accessing a function without () returns the whole function, not the result: let value = toCelsius;

  • Functions can be used the same way as variables, in all types of formulas, assignments, and calculations.

Objects

  • const name = {property:property name, ... }

  • A method is a function stored as a property.

  • this keyword

  • do not declare Strings, numbers, and booleans as object

Events

  • Js event is how Js react when sth happen to Html elements (click a button, web page finished loading, ...)

  • event are commonly used in function rather than directly used

String

  • let string = string.length;

  • turn special characters to string: \' ~ ' ; \" ~ "

String method

Extract string parts:

  • slice(start, end); :

    • if 'end' is omitted, the method will slice out from 'start' to the rest

    • if 'end' is omitted, 'start' is negative, the position will be counted from the end

    • 'end' and 'start' is negative works like positive

  • substring(start, end); : works as slice but negative values are treated as 0

  • substr(start, length); similar to slice

Replace string

let text = "Please visit Microsoft!"; let newText = text.replace("Microsoft", "W3Schools");

  • replaces a specified value with another value in a string; returns a new string, does not change the string it is called on; and only replace the first match; case sensitive

  • use /specified value/i flag to remove case sensitive

  • use /specified value/g flag to replace all matches

  • text = text.replaceAll("Cats","Dogs");

Uppercase / Lowercase

  • toUpperCase() : the whole string to uppercase

  • toLowerCase() ; the whole string to lower case

Concat

  • join 2 or more string

let text3 = text1.concat(" ", text2);

Trim

  • remove whitespace from head and tail

  • trimStart() / trimEnd() : remove whitespace from head or tail only

String padding

  • padStart() / padEnd(): pad head or tail with character "y" until reach length x

let padded = text.padStart(x,"y");

  • to padding number, it needed to convert to string first

let numb = 5;

let text = numb.toString();

let padded = text.padStart(4,"0");

Extracting characters

  • charAt(position) : return the character

  • charCodeAt(position) : return the UTF-16 code of the character

  • indexOf() return the first position of a string in a string; search forward

  • lastIndexOf() return the last position of a string in a string; search backward

  • Both indexOf(), and lastIndexOf() return -1 if the text is not found

  • a second parameter is added to declare the position to start searching:

text.lastIndexOf("locate", 15);

  • search()

String match

  • .match("string"); : returns an array containing the results of matching a string against a string

  • .match(/ain/gi); : use /g , /i flag to find all matches despite uppercase or lowercase

  • .matchAll() : return every matches in order

  • .includes("specified value", position) : return true / false whether the value is included or not, search from position forward (optional); case sensitive

  • .startsWith() : return True / false; check if the string starts with specified value or not; a second parameter to set the start position (optional); case sensitive

  • .endsWith()

Numbers

Integer Precision

  • Integers (numbers without a period or exponent notation) are accurate up to 15 digits

Floating Precision

  • Floating point arithmetic is not always 100% accurate; to solve this, multiply the parameter, calculate then divide

Not a Number

  • do arithmetic with a non-numeric string result in NaN

  • isNaN() check a value is or isn't a number

  • typeof NaN is number

Infinity (or -Infinity)

  • the value return when calculate a number outside the largest possible number

  • Infinity is a number

Hexadecimal

  • hexadecimal constant preceded by 0x

  • .toString() : to convert number among bases (base 2, base 10, base 8, ...)

Numbers as Objects

  • numbers can be defined as objects but DON'T

  • objects can't be compared with objects

  • compare 2 objects alway return false

Number Method

  • .toString() : convert number to string

  • .toExponential(x) : rounded number to x numbers behind decimal point

  • .toFixed(x) : number of decimal

  • .toPrecision(x) : number with length x

  • .valueOf() : return a number as a number

  • Number() : convert JavaScript variables to numbers

  • parseInt() : parse a string and return the first number, float not included

  • parseFloat() : parse a string and return the first number

Last updated