File tree Expand file tree Collapse file tree 8 files changed +199
-0
lines changed Expand file tree Collapse file tree 8 files changed +199
-0
lines changed Original file line number Diff line number Diff line change
1
+ let arr1 = [ "A" , true , 2 ] ;
2
+
3
+ //Push and pop
4
+ console . log ( "\n********Push and pop********\n" ) ;
5
+ console . log ( arr1 . push ( "new value" ) ) ;
6
+ console . log ( arr1 ) ;
7
+ console . log ( arr1 . pop ( ) ) ; //Remove last value
8
+ console . log ( arr1 ) ;
9
+
10
+ //Shift and unshift
11
+ console . log ( "\n********Shift and unshift********\n" ) ;
12
+ console . log ( arr1 . unshift ( "new value" ) ) ;
13
+ console . log ( arr1 ) ;
14
+ console . log ( arr1 . shift ( ) ) ; //Remove first value
15
+ console . log ( arr1 ) ;
16
+
17
+ //Concat
18
+ console . log ( "\n********Concat********\n" ) ;
19
+ let arr2 = [ "B" , false , 3 ] ;
20
+ let newArr = arr1 . concat ( arr2 ) ;
21
+ let newArr2 = arr2 . concat ( [ 1 , 2 , 3 ] ) ;
22
+ console . log ( newArr ) ;
23
+ console . log ( newArr2 ) ;
24
+
Original file line number Diff line number Diff line change
1
+ //Creating an array
2
+ console . log ( "\n********Creating an array********\n" ) ;
3
+
4
+ let arrayLength = 5 ;
5
+ let arr1 = [ ] ;
6
+ let arr2 = Array ( arrayLength ) ;
7
+
8
+ //Getting array length
9
+ console . log ( "\n********Getting array length********\n" ) ;
10
+
11
+ console . log ( arr1 . length ) ;
12
+ console . log ( arr2 . length ) ;
Original file line number Diff line number Diff line change
1
+ //Adding data during creation
2
+ console . log ( "\n********Adding data during creation********\n" ) ;
3
+ let arr1 = [ "A" , true , 2 ] ;
4
+ console . log ( arr1 [ 0 ] ) ;
5
+ console . log ( arr1 [ 1 ] ) ;
6
+
7
+ //Adding data after creation
8
+ console . log ( "\n********Adding data after creation********\n" ) ;
9
+ let arrayLength = 2 ;
10
+ let arr2 = Array ( arrayLength ) ;
11
+ arr2 [ 0 ] = "Value at index 0" ;
12
+ console . log ( arr2 [ 0 ] ) ;
13
+ console . log ( arr2 [ 1 ] ) ; //No value present at index
14
+
15
+ //Length and index
16
+ console . log ( "\n********Length and index********\n" ) ;
17
+
18
+ console . log ( arr1 . length ) ;
19
+ console . log ( arr1 [ 3 ] ) ; //Doesn't exist
20
+ console . log ( arr1 [ 2 ] ) ; //Last index of array
21
+ console . log ( arr1 [ arr1 . length - 1 ] ) ; //Equals 2
22
+
23
+ console . log ( "\n********Length and index Part 2********\n" ) ;
24
+ let arr3 = Array ( 3 ) ; //Empty array of length 3
25
+ arr3 [ 2 ] = "Adding a value!" ;
26
+ console . log ( arr3 [ 2 ] ) ; //Last index of array
27
+ console . log ( arr3 [ arr3 . length - 1 ] ) ; //Index: 2
28
+ console . log ( arr3 [ 0 ] ) ; //No value yet
29
+ console . log ( arr3 [ 1 ] ) ; //No value yet
Original file line number Diff line number Diff line change
1
+ function criticalCode ( ) {
2
+ throw "throwing an exception" ;
3
+ }
4
+
5
+ function logError ( theException ) {
6
+ console . log ( theException ) ;
7
+ }
8
+
9
+ //Throwing Exceptions
10
+ console . log ( "\n********Throwing Exceptions********\n" ) ;
11
+
12
+ throw 'myException' ;
13
+ throw true ;
14
+
15
+ //Try..Catch
16
+ console . log ( "\n********Try..Catch********\n" ) ;
17
+
18
+ try {
19
+ criticalCode ( ) ;
20
+ } catch ( ex ) {
21
+ console . log ( "Got an error" ) ;
22
+ logError ( ex ) ;
23
+ }
24
+
25
+ //Throwing in Try..Catch
26
+ console . log ( "\n********Throwing in Try..Catch********\n" ) ;
27
+
28
+ try {
29
+ throw "An exception that is thrown every time" ;
30
+
31
+ } catch ( ex ) {
32
+ console . log ( "Got an error" ) ;
33
+ logError ( ex ) ;
34
+ }
35
+
36
+ //Try..Catch..Finally
37
+ console . log ( "\n********Try..Catch..Finally********\n" ) ;
38
+
39
+ try {
40
+ criticalCode ( ) ;
41
+ } catch ( ex ) {
42
+ console . log ( "Got an error" ) ;
43
+ logError ( ex ) ;
44
+ } finally {
45
+ console . log ( "Code that always will run" ) ;
46
+ }
Original file line number Diff line number Diff line change
1
+ let num1 = '150' ;
2
+ let flo1 = '1.50' ;
3
+
4
+ console . log ( "********Converting strings to integers********" ) ;
5
+ //Converting strings to integers
6
+ console . log ( parseInt ( '100' ) ) ;
7
+ console . log ( parseInt ( num1 ) ) ;
8
+ console . log ( parseInt ( 'ABC' ) ) ;
9
+ console . log ( parseInt ( '0xF' ) ) ; //Hexadecimal number
10
+
11
+ console . log ( "********Converting strings to float********" ) ;
12
+ //Converting strings to float
13
+ console . log ( parseFloat ( '1.00' ) ) ;
14
+ console . log ( parseFloat ( flo1 ) ) ;
15
+ console . log ( parseFloat ( 'ABC' ) ) ;
16
+
17
+ console . log ( "********More Conversion Examples********" ) ;
18
+ //More Conversion Examples
19
+ //Numbers after special characters are ignored
20
+ console . log ( parseInt ( '1.5' ) ) ;
21
+ console . log ( parseInt ( '1 + 1' ) ) ;
22
+
23
+ //Using Template Literals
24
+ console . log ( parseInt ( `${ 1 + 1 } ` ) ) ;
25
+
26
+ console . log ( "********Converting numbers to strings********" ) ;
27
+ //Converting numbers to strings
28
+ console . log ( num1 . toString ( ) ) ;
29
+ console . log ( flo1 . toString ( ) ) ;
30
+ console . log ( ( 100 ) . toString ( ) ) ;
Original file line number Diff line number Diff line change
1
+ let num1 = 100 ;
2
+
3
+ //Basic Math
4
+ console . log ( "********Basic Math********" ) ;
5
+ console . log ( num1 + 25 ) ;
6
+ console . log ( num1 - 100 ) ;
7
+ console . log ( num1 * 100 ) ;
8
+ console . log ( num1 / 1500 ) ;
9
+
10
+ console . log ( "********Additional Mathematical Operations********" ) ;
11
+ //Additional Mathematical Operations
12
+ console . log ( num1 % 1500 ) ; // Remainder
13
+ console . log ( ++ num1 ) ; //Increment
14
+ console . log ( -- num1 ) ; //Decrement
15
+
16
+ console . log ( "********Using the Math Object********" ) ;
17
+ //Using the Math Object
18
+ console . log ( Math . PI ) ; //Pi
19
+ console . log ( Math . sqrt ( num1 ) ) ; //Square root
Original file line number Diff line number Diff line change
1
+ let str1 = "Hello" ;
2
+ let str2 = "World!" ;
3
+
4
+ //Using the + operator
5
+ console . log ( "********Using the + operator********\n" ) ;
6
+ console . log ( str1 + str2 ) ;
7
+ console . log ( str1 + "Big" + str2 ) ;
8
+
9
+ console . log ( "\n********Adding Spacing********\n" ) ;
10
+ //Adding Spacing
11
+ str1 = "Hello " ;
12
+
13
+ console . log ( str1 + str2 ) ;
14
+ console . log ( str1 + "Big " + str2 )
15
+
16
+ console . log ( "\n********Be careful with numbers!********\n" ) ;
17
+ //Be careful with numbers!
18
+ let num1 = 1 ;
19
+ let num2 = "1" ;
20
+
21
+ console . log ( num1 + num2 ) ;
22
+ console . log ( num1 + 1 ) ;
Original file line number Diff line number Diff line change
1
+ //Concatenation with template literals
2
+ console . log ( "\n********Concatenation with template literals********\n" ) ;
3
+
4
+ let str1 = "JavaScript" ;
5
+ let str2 = "fun" ;
6
+
7
+ console . log ( `I am writing code in ${ str1 } ` ) ;
8
+ console . log ( `Formatting in ${ str1 } is ${ str2 } !` ) ;
9
+
10
+ //Expressions in template literals
11
+ console . log ( "\n********Expressions in template literals********\n" ) ;
12
+
13
+ let bool1 = true ;
14
+ console . log ( `1 + 1 is ${ 1 + 1 } ` ) ;
15
+ console . log ( `The opposite of true is ${ ! bool1 }
16
+
17
+ ` )
You can’t perform that action at this time.
0 commit comments