Skip to content

Commit 429744a

Browse files
feat: add global functions
1 parent fcf50b2 commit 429744a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
INFO: Global Functions
3+
These can be called directly, like parseInt() or isNaN(), without needing an object like MATH. or JSON.
4+
*/
5+
6+
/*
7+
INFO: parseInt(string, radix)
8+
1. Converts a string to an integer
9+
2. radix is the base (like 10 for decimal, 2 for binary)
10+
*/
11+
parseInt("42"); // 42
12+
parseInt("101", 2); // 5 (binary to decimal)
13+
parseInt("10.5"); // 10 (ignores decimal)
14+
parseInt("10abc"); // 10 (ignores alphabet)
15+
parseInt("abc"); // NaN
16+
17+
/*
18+
INFO: parseFloat(string)
19+
Converts a string to a floating point number.
20+
*/
21+
parseFloat("3.14"); // 3.14
22+
parseFloat("10.00"); // 10
23+
parseFloat("10abc"); // 10
24+
parseFloat("abc"); // NaN
25+
26+
/*
27+
INFO: isNaN(value)
28+
1. Checks if a value is NaN (Not a Number)
29+
2. isNaN() tries to coerce non-numbers into number
30+
*/
31+
isNaN("hello"); // true
32+
isNaN(123); // false
33+
isNaN("123"); // false -> "123" is coerced to number
34+
inNaN(NaN); // true
35+
36+
/*
37+
INFO: isFinite()
38+
Checks if a value is a finite number (not Infinity, -Infinity or NaN)
39+
*/
40+
isFinite(100); // true
41+
isFinite("50"); // true (coerced)
42+
isFinite(Infinity); // false
43+
isFinite(NaN); // false
44+
45+
/*
46+
INFO: encodeURIComponent(str)
47+
1. Encodes a URI component (useful for query strings, URLs).
48+
2. Replace unsafe characters with escape sequences.
49+
*/
50+
const name = "rafay & team";
51+
const safe = encodeURIComponent(name);
52+
console.log(safe); // "rafay%20%26%20team"
53+
54+
/*
55+
INFO: decodeURIComponent(str)
56+
Reverses encodeURIComponent()
57+
*/
58+
decodeURIComponent("rafay%20%26%20team"); // "refay & team"
59+
60+
/*
61+
INFO: String(value)
62+
Converts any value to a string
63+
*/
64+
String(123); // "123"
65+
String(true); // "true"
66+
String(null); // "null"
67+
68+
/*
69+
INFO: Number(value)
70+
Converts to number, if possbile.
71+
*/
72+
Number("123"); // 123
73+
Number("abc"); // NaN
74+
Number(true); // 1
75+
Number(false); // 0
76+
77+
/*
78+
INFO: Boolean(value)
79+
Converts to true or false.
80+
*/
81+
Boolean(0); // false
82+
Boolean("hello"); // true
83+
Boolean(""); // false
84+
Boolean(null); // false
85+
86+
/*
87+
INFO: eval(string)
88+
Executes javascript code from a string
89+
*/
90+
eval("2 + 2"); // 4
91+
92+
/*
93+
INFO: typeof
94+
Not a function, but an operator that's super helpful.
95+
*/
96+
typeof "hello"; // string
97+
typeof 123; // number
98+
typeof null; // object
99+
typeof undefined; // undefined
100+
101+
/*
102+
INFO: Infinity, NaN, undefined
103+
These are global values, not Functions
104+
*/
105+
console.log(Infinity); // Infinity
106+
console.log(NaN); // NaN
107+
console.log(undefined); // undefined

0 commit comments

Comments
 (0)