Repository navigation
Expand file tree
/
Copy pathspecialfunction.js
More file actions
47 lines (38 loc) · 1.66 KB
/
Copy pathspecialfunction.js
File metadata and controls
47 lines (38 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// .lenth .......1
console.log("hello world" .length);
//.lenth we can use to find the length of a string
//parseInt parseFloat........2
console.log(parseInt("123abc")); // 123
const numStr = "123";
const num = parseInt(numStr);
console.log(num);
// parseint used to convert a string into int , float , number
//.slice(1)......3
console.log("hello world".slice(1)); // "ello world"
// .slice(1) used to extract a part of a string starting from index given (1)
// it used to remove the first character of the string like removing symbol of currency $100
// number // string to number strict......4
console.log(Number("123")); // 123
console.log(Number("123abc")); // NaN
//differnces between parseInt and Number:......5
// parseInt("123abc") returns 123 because:
// It parses from left to right
// Stops when it encounters the first non-numeric character
// Returns the valid number it found before stopping
// Number("123abc") returns NaN because:
// It requires the entire string to be a valid number
// Since "123abc" contains non-numeric characters, it's not a valid number as a whole
// Therefore returns NaN (Not a Number)
// unary operators .......6
console.log(+ "123"); // 123
//it used to convert a string into number
console.log(- "123"); // -123
//it used to convert a string into negative number
console.log(+"123abc"); // NaN
//it used to convert a string into number but since it has non-numeric characters it returns
//prefix and postfix increment
let x = 5;
console.log(++x); // 6 (prefix: increments x to 6, then returns 6)
console.log(x); // x is now 6
console.log(x++); // 6 (postfix: returns the current value 6, THEN increments x to 7)
console.log(x); // x is now 7