-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript.txt
170 lines (146 loc) · 7.23 KB
/
JavaScript.txt
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
JavaScript code must be inserted between <script> and </script> tags.
JavaScript HTML methods
* getElementById()
"src" Attribute is used to specify the URL of external JavaScript file
* <script src="myScript.js"></script>
JavaScript Display Possibilities
Writing into an HTML element, using innerHTML //document.getElementById("demo").innerHTML = 5 + 6;
Writing into the HTML output using document.write(). //document.write(5 + 6);
Writing into an alert box, using window.alert(). //window.alert(5 + 6);
Writing into the browser console, using console.log(). //console.log(5 + 6); function func() { return (5 * 19); } console.log(func());
JavaScript Keywords
Keyword Description
break Terminates a switch or a loop
continue Jumps out of a loop and starts at the top
debugger Stops the execution of JavaScript, and calls (if available) the debugging function
do ... while Executes a block of statements, and repeats the block, while a condition is true
for Marks a block of statements to be executed, as long as a condition is true
function Declares a function
if ... else Marks a block of statements to be executed, depending on a condition
return Exits a function
switch Marks a block of statements to be executed, depending on different cases
try ... catch Implements error handling to a block of statements
var Declares a variable
arithmetic operators ( + - * / )
assignment operator ( = )
Code after double slashes // or between /* and */ is treated as a comment.
Names can also begin with $ and _
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
Operator Example Same As
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y
**= x **= y x = x ** y
JavaScript Data Types
JavaScript variables can hold many data types: numbers, strings, objects and more:
var length = 16; // Number
var lastName = "Johnson"; // String
var x = {firstName:"John", lastName:"Doe"}; // Object
JavaScript Arrays
var cars = ["Saab", "Volvo", "BMW"];
JavaScript Objects
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
* accessing objects : objectName.propertyName
* "this" Keyword : this refers to the "owner"; Eg : this.firstName + " " + this.lastName;
* Do Not Declare Strings, Numbers, and Booleans as Objects!
JS EVENTS
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page
JavaScript Strings
A JavaScript string is zero or more characters written inside quotes.
var x = "John Doe";
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator
String Methods and Properties
String Length
var sln = txt.length;
Finding a String in a String
var pos = str.indexOf("locate");
JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third ...
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
var pos = str.lastIndexOf("locate");
Both indexOf(), and lastIndexOf() return -1 if the text is not found.
The search() method cannot take a second start position argument.
Extracting String Parts
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substr(start, length)
The slice() Method
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end not included).
This example slices out a portion of a string from position 7 to position 12 (13-1):
Example
var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13);
The result of res will be:
Banana
Remember: JavaScript counts positions from zero. First position is 0.
The substring() Method
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
Example
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);
The result of res will be:
Banana
If you omit the second parameter, substring() will slice out the rest of the string.
Replacing String Content
The replace() method replaces a specified value with another value in a string:
Example
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3Schools");
The replace() method does not change the string it is called on. It returns a new string.
By default, the replace() method replaces only the first match:
Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase():
Example
var text1 = "Hello World!"; // String
var text2 = text1.toUpperCase(); // text2 is text1 converted to upper
A string is converted to lower case with toLowerCase():
String.trim()
The trim() method removes whitespace from both sides of a string:
Example
var str = " Hello World! ";
charAt(position)
charCodeAt(position)
Property access [ ]
The charAt() Method
The charAt() method returns the character at a specified index (position) in a string:
Example
var str = "HELLO WORLD";
str.charAt(0); // returns H
The charCodeAt() Method
The charCodeAt() method returns the unicode of the character at a specified index in a string:
Converting a String to an Array
A string can be converted to an array with the split() method:
var txt = "a,b,c,d,e"; // String
txt.split(","); // Split on commas
txt.split(" "); // Split on spaces
txt.split("|"); // Split on pipe