-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code-Snippet.txt
187 lines (123 loc) · 4.1 KB
/
Code-Snippet.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
1) Remove an item from the Array
const array = [2, 5, 9];
console.log(array); // [2, 5, 9]
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
console.log(array); // [2, 9]
2)
Scenario 1: [NO STRICT MODE]
var city = "Chicago"
console.log(city) // Prints the city name, i.e. Chicago
Scenario 2: [NO STRICT MODE]
city = "Chicago"
console.log(city) // Prints the city name, i.e. Chicago
Scenario 3: [STRICT MODE]
'use strict';
city = "Chicago"
console.log(city) // Reference Error: asignment is undeclared variable city.
3) A closure is a pairing of:
A function, and
A reference to that function's outer scope (lexical environment)
A lexical environment is part of every execution context (stack frame) and is a map between identifiers (ie. local variable names) and values.
Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.
If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.
In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:
function foo() {
const secret = Math.trunc(Math.random()*100)
return function inner() {
console.log(`The secret number is ${secret}.`)
}
}
const f = foo() // `secret` is not directly accessible from outside `foo`
f() // The only way to retrieve `secret`, is to invoke `f`
4) To check whether a string contains a substring:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
5)
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`); // "The word "fox" is in the sentence"
6) Remove a property from a JavaScript object:
delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];
----------------------
ar myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject); // {"ircEvent": "PRIVMSG", "method": "newURI"}
7)
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
== operator just compares the values not datatype.
=== operator compare the values with comparison of its datatype.
Example :
1 == "1" //true
1 === "1" //false
8) "let" and "var" difference :
function run() {
var foo = "Foo";
let bar = "Bar";
console.log(foo, bar); // Foo Bar
{
var moo = "Mooo"
let baz = "Bazz";
console.log(moo, baz); // Mooo Bazz
}
console.log(moo); // Mooo
console.log(baz); // ReferenceError
}
run();
------------------------------------
function run() {
console.log(foo); // undefined
var foo = "Foo";
console.log(foo); // Foo
}
run();function checkHoisting() {
console.log(foo); // ReferenceError
let foo = "Foo";
console.log(foo); // Foo
}
checkHoisting();
---------------------------------------
function checkHoisting() {
console.log(foo); // ReferenceError
let foo = "Foo";
console.log(foo); // Foo
}
checkHoisting();
9)
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry); // a b c
});
10) Check whether "Checkbox" is checked or not:
$("#isAgeSelected").on('change', function() {
if ($("#isAgeSelected").is(':checked'))
alert("checked");
else {
alert("unchecked");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected" />
<div id="txtAge" style="display:none">
Age is selected
</div>