MYSQL | Important Questions & Functionality
1. Difference Between var , let and const variables
var | let | const |
---|---|---|
The scope of a var variable is functional or global scope. | The scope of a let variable is block scope. | The scope of a const variable is block scope. |
It can be updated and re-declared in the same scope. | It can be updated but cannot be re-declared in the same scope. | It can neither be updated or re-declared in any scope. |
It can be declared without initialization. | It can be declared without initialization. | It cannot be declared without initialization. |
It can be accessed without initialization as its default value is “undefined”. | It cannot be accessed without initialization otherwise it will give ‘referenceError’. | It cannot be accessed without initialization, as it cannot be declared without initialization. |
These variables are hoisted. | These variables are hoisted but stay in the temporal dead zone untill the initialization. | These variables are hoisted but stays in the temporal dead zone until the initialization. |
2. Loop
JavaScript supports different kinds of loops:
- for - loops through a block of code a number of times
- for/in - loops through the properties (Keys) of an object
- for/of - loops through the values of an iterable object
- while - loops through a block of code while a specified condition is true
- do/while - also loops through a block of code while a specified condition is true
1. for : loops through a block of code a number of times
Syntax:
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
// code block to be executed
}
Example:
<p id="demo"></p>
<script>
let text = "";
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
Output:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 1
The number is 2
The number is 3
The number is 4
2. for/in : loops through the properties (Keys) of an object
Syntax:
for (key in object) {
// code block to be executed
}
// code block to be executed
}
Example:
<p id="demo"></p>
<script>
const person = {fname:"John", lname:"Doe", age:25};
let text = "";
for (let x in person) {
text += person[x];
}
let text = "";
for (let x in person) {
text += person[x];
}
document.getElementById("demo").innerHTML = txt;
</script>
Output:
John Doe 25
3. for/of : The for of statement loops through the values of any iterable object:
Syntax:
for (variable of array) {
// code block to be executed
}
// code block to be executed
}
Example:
<p id="demo"></p>
<script>
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x;
}
let text = "";
for (let x of cars) {
text += x;
}
document.getElementById("demo").innerHTML = text;
</script>
Output:
BMW
Volvo
Mini
Volvo
Mini
4. while : Loops can execute a block of code as long as a specified condition is true.
Syntax:
while (condition) {
// code block to be executed
}
// code block to be executed
}
Example:
<p id="demo"></p>
<script>
let text = "";
let i = 0;
while (i < 10) {
text += "The number is " + i;
i++;
}
text += "The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
Output:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
5. do/while : also loops through a block of code while a specified condition is true
Syntax:
do {
// code block to be executed
}
while (condition);
// code block to be executed
}
while (condition);
Example:
<p id="demo"></p>
<script>
let text = ""
let i = 0;
do {
text += "The number is " + i;
i++;
}
while (i < 10);
text += "The number is " + i;
i++;
}
while (i < 10);
document.getElementById("demo").innerHTML = text;
</script>
Output:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
2. Some Important Array Function and Method
1. Array.forEach(): The forEach() method calls a function (a callback function) once for each array element.
<script>
const numbers = [45, 4, 9, 16, 25];
let sum = 0;
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = sum;
function myFunction(value, index, array) {
sum += value ;
}
</script>
2. Array.join(): The join() method returns an array as a string. The join() method does not change the original array. Any separator can be specified. Separator is optional and the default is comma (,).
Syntax:
array.join(separator)
Example:
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join(); //output: Banana,Orange,Apple,Mango
let text = fruits.join(" and "); //output: Banana and Orange and Apple and Mango
document.getElementById("demo").innerHTML = text;
</script>
3. Ajax Form Submission
$(document).ready(function(){
//Create Form
$('#createStatsForm').submit(function(event){
event.preventDefault();
$("#submitBtn").attr('disabled', true);
var form=$("#createStatsForm")[0];
var data= new FormData(form);
//data.append('header', headerValue); //append data by append function
$.ajax({
type: "POST",
url: "{{url('dashboard')}}",
headers: {
"X-CSRF-TOKEN": "{{csrf_token()}}"
},
data: data,
processData: false,
contentType: false,
success: function(data){
if(data.status == 200){
closeModel();
console.log(data);
}
else if(data.status == 400){
showError(data.message, "showMessage");
if($("#submitBtn").length) {
$("#submitBtn").attr('disabled', false);
}
}
else{
console.log(data);
}
},
error: function(e){
console.log(e.responseText);
}
});
});
3. Add Event Listener in Ajax
document.getElementById(element_id).onkeyup = function() {function_name()}; //first way
document.getElementById(element_id).addEventListener("keyup", function_name()); //second way
2. Break and Continue
1. Break Statement: You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch() statement.
The break statement can also be used to jump out of a loop.
Example:
<p id="demo"></p>
<script>
let text = "";
for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
Output:
The number is 0
The number is 1
The number is 2
The number is 1
The number is 2
1. Continue Statement: The
continue
statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.Example:
<p id="demo"></p>
<script>
let text = "";
for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
Output:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
0 Comments