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 structundefined: a var without value hold value
undefined
and typeundefined
; string without value hold value""
and typeundefined
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
keyworddo 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 sensitiveuse
/specified value/g
flag to replace all matchestext = 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 charactercharCodeAt(
position
) :
return the UTF-16 code of the character
String search
indexOf()
return the first position of a string in a string; search forwardlastIndexOf()
return the last position of a string in a string; search backwardBoth
indexOf()
, andlastIndexOf()
return -1 if the text is not founda 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 numbertypeof 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 numberNumber()
: convert JavaScript variables to numbersparseInt()
: parse a string and return the first number, float not includedparseFloat() : parse a string and return the first number
Last updated