-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOCS_JS_Basics
134 lines (100 loc) · 3.84 KB
/
DOCS_JS_Basics
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
# Variable Definition
The "let" keyword is used in declaring local variables.
Event Handlers
Anonymous Functions
document.querySelector("html").addEventListener("click", function () {
alert("Ouch! Stop poking me!");
});
2. Arrow Function
document.querySelector("html").addEventListener("click", () => {
alert("Ouch! Stop poking me!");
});
# Methods of Assigning Anonymous Function
const myImage = document.querySelector("img");
myImage.onclick = () => {
const mySrc = myImage.getAttribute("src");
if (mySrc === "images/firefox-icon.png") {
myImage.setAttribute("src", "images/firefox2.png");
} else {
myImage.setAttribute("src", "images/firefox-icon.png");
}
};
# Using the WebStorage API to store values in a dictionary
# N/B How to prompt for user input using the prompt() - Function
const myName = prompt("Please enter your name.");
localStorage.setItem("name", myName);
# Methods of Assigning Anonymous Function
const myImage = document.querySelector("img");
myImage.onclick = () => {
const mySrc = myImage.getAttribute("src");
if (mySrc === "images/firefox-icon.png") {
myImage.setAttribute("src", "images/firefox2.png");
} else {
myImage.setAttribute("src", "images/firefox-icon.png");
}
};
# Using the WebStorage API to store values in a dictionary
# N/B How to prompt for user input using the prompt() - Function
const myName = prompt("Please enter your name.");
localStorage.setItem("name", myName);
# Methods of Assigning Anonymous Function
const myImage = document.querySelector("img");
myImage.onclick = () => {
const mySrc = myImage.getAttribute("src");
if (mySrc === "images/firefox-icon.png") {
myImage.setAttribute("src", "images/firefox2.png");
} else {
myImage.setAttribute("src", "images/firefox-icon.png");
}
};
# Using the WebStorage API to store values in a dictionary
# N/B How to prompt for user input using the prompt() - Function
const myName = prompt("Please enter your name.");
dict(key:value)
localStorage.setItem("name", myName);
if(!localStorage.getItem('name')) {
setUserName();
} else {
let storedName = localStorage.getItem('name');
myHeading.innerHTML = 'Mozilla is cool, ' + storedName;
}
# Concepts:
Dynamic Typing - means that the language does not have to know the data type of a variable prior to declaration/definition.
Therefore: let myAge = 22 --> Does not require the datatype ::int::
Weak Typing - Javascript enables for implicit type conversions without throwing off error for example;
const foo = 42
const result = foo + "1"
console.log(result) # will output the string "421"
# The program will convert the integer value into a string rather than the inverse.
##############33 Robust test for null and undefined Primitive Values ######
# more Robust test is to combine check for null and undefined variable values
if (!Object.is(myVar, undefined) && !Object.is(myVar,null) {
console.log("myVar is DEFINED");
}
# which reads as - print myVar is DEFINED -> only when
#its value is defined and its value is not null
#############3333 Primitive Wrapper Functions #############
num = 2.12555
2.12555
num.toPrecision(4)
'2.126'
num.toString()
'2.12555'
num.toLocaleString()
'2.126'
## String Primitives
let myName = "hello"
undefined
myName.toLocaleLowerCase()
'hello'
myName.toUpperCase()
'HELLO'
myName.toLocaleUpperCase()
'HELLO'
myName.fontsize(44)
'<font size="44">hello</font>'
myName.trim[end,left,start] // Values within the brackets are optional
#############3 Key Note on Strings that may be used to Optimize Memory Storage ###########3
JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it. String methods create new strings based on the content of the current string — for example:
A substring of the original using substring().
A concatenation of two strings using the concatenation operator (+) or concat().