You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| document.anchors | Returns all elements that have a name attribute | 1
2009
+
| document.anchors | Returns all elements that have a name attribute | 1 |
2010
2010
| document.applets | Deprecated | 1 |
2011
2011
| document.baseURI | Returns the absolute base URI of the document | 3 |
2012
2012
| document.body | Returns the element | 1 |
@@ -3034,32 +3034,57 @@ function showPosition(position) {
3034
3034
3035
3035
## AJAX
3036
3036
3037
-
**XMLHttp** -
3037
+
AJAX (Asynchronous JavaScript and XML) is a technique used in web development to create asynchronous web applications. It allows you to update parts of a web page without reloading the entire page. Originally, XML (eXtensible Markup Language) was commonly used for data exchange, but modern AJAX implementations often use JSON (JavaScript Object Notation) for data interchange due to its lighter syntax.
3038
3038
3039
-
```javascript
3039
+
**XMLHttp** - The XMLHttpRequest object is used to interact with servers asynchronously. Here's an example of creating an XMLHttpRequest object
3040
3040
3041
+
```javascript
3042
+
let xhttp =newXMLHttpRequest();
3041
3043
```
3042
3044
3043
-
**Request** -
3045
+
**Request** - To make a request to a server, you'll typically use methods like open() and send()
3044
3046
3045
3047
```javascript
3046
-
3048
+
xhttp.open('GET', 'https://api.example.com/data', true); // Method, URL, async (true or false)
3049
+
xhttp.send(); // Send the request
3047
3050
```
3048
3051
3049
-
**Response** -
3052
+
You can also send data along with the request by passing parameters within the send() method.
3050
3053
3051
-
```javascript
3054
+
**Response** - Handling the response from the server can be done using event listeners like onreadystatechange and checking for the readyState and status of the request
3052
3055
3056
+
```javascript
3057
+
xhttp.onreadystatechange=function() {
3058
+
if (this.readyState===4&&this.status===200) {
3059
+
// Handle successful response
3060
+
console.log(this.responseText); // Response data
3061
+
} else {
3062
+
// Handle errors
3063
+
console.error('There was a problem with the request.');
3064
+
}
3065
+
};
3053
3066
```
3054
3067
3055
-
**XML File** -
3068
+
**XML File** - If you are working with an XML file, you can parse the response using methods like responseXML to access the XML data
3056
3069
3057
3070
```javascript
3058
-
3071
+
xhttp.onreadystatechange=function() {
3072
+
if (this.readyState===4&&this.status===200) {
3073
+
let xmlDoc =this.responseXML;
3074
+
// Process xmlDoc for XML data
3075
+
console.log(xmlDoc);
3076
+
}
3077
+
};
3059
3078
```
3060
3079
3061
3080
## JSON
3062
3081
3082
+
JSON stands for JavaScript Object Notation
3083
+
3084
+
JSON is a text format for storing and transporting data
3085
+
3086
+
JSON is "self-describing" and easy to understand
3087
+
3063
3088
### JSON Methods
3064
3089
3065
3090
parse() - Parses a JSON string and returns a JavaScript object
@@ -3079,34 +3104,39 @@ Data Types
3079
3104
3080
3105
```
3081
3106
3082
-
**Parse** -
3083
-
3084
-
```javascript
3085
-
3086
-
```
3087
-
3088
-
**Stringify** -
3107
+
**Parse** - The parse() method is used to parse a JSON string and convert it into a JavaScript object.
**Objects** - Objects in JSON are key-value pairs enclosed in curly braces {}.
3101
3124
3102
3125
```javascript
3103
-
3126
+
constperson= {
3127
+
"name":"Alice",
3128
+
"age":30,
3129
+
"address": {
3130
+
"city":"New York",
3131
+
"zipcode":"10001"
3132
+
}
3133
+
};
3104
3134
```
3105
3135
3106
-
**Server** -
3136
+
**Arrays** - Arrays in JSON are ordered lists of values enclosed in square brackets [].
3107
3137
3108
3138
```javascript
3109
-
3139
+
constcolors= ["Red", "Green", "Blue"];
3110
3140
```
3111
3141
3112
3142
## JQuery
@@ -3884,15 +3914,206 @@ newObj();
3884
3914
3885
3915
## Snippets
3886
3916
3887
-
```jsx
3917
+
Here are a few JavaScript snippets that you might find useful:
3888
3918
3889
-
```
3919
+
1. **Hello World:**
3890
3920
3891
-
## Short Hands
3921
+
```javascript
3922
+
console.log("Hello, World!");
3923
+
```
3892
3924
3893
-
```jsx
3925
+
2. **Variable Declaration:**
3894
3926
3895
-
```
3927
+
```javascript
3928
+
let variableName ="Some value";
3929
+
```
3930
+
3931
+
3. **Conditional Statement (if-else):**
3932
+
3933
+
```javascript
3934
+
let condition =true;
3935
+
3936
+
if (condition) {
3937
+
console.log("Condition is true");
3938
+
} else {
3939
+
console.log("Condition is false");
3940
+
}
3941
+
```
3942
+
3943
+
4. **Loop (for):**
3944
+
3945
+
```javascript
3946
+
for (let i =0; i <5; i++) {
3947
+
console.log(i);
3948
+
}
3949
+
```
3950
+
3951
+
5. **Function Declaration:**
3952
+
3953
+
```javascript
3954
+
functiongreet(name) {
3955
+
console.log("Hello, "+ name +"!");
3956
+
}
3957
+
3958
+
greet("John");
3959
+
```
3960
+
3961
+
6. **Array Manipulation:**
3962
+
3963
+
```javascript
3964
+
let fruits = ['Apple', 'Banana', 'Orange'];
3965
+
3966
+
// Add an element to the end
3967
+
fruits.push('Mango');
3968
+
3969
+
// Remove the last element
3970
+
fruits.pop();
3971
+
3972
+
// Access elements by index
3973
+
console.log(fruits[1]);
3974
+
```
3975
+
3976
+
7. **Object Declaration:**
3977
+
3978
+
```javascript
3979
+
let person = {
3980
+
name:'John',
3981
+
age:25,
3982
+
profession:'Developer'
3983
+
};
3984
+
3985
+
// Accessing object properties
3986
+
console.log(person.name);
3987
+
```
3988
+
3989
+
8. **Async/Await:**
3990
+
3991
+
```javascript
3992
+
asyncfunctionfetchData() {
3993
+
try {
3994
+
let response =awaitfetch('https://api.example.com/data');
3995
+
let data =awaitresponse.json();
3996
+
console.log(data);
3997
+
} catch (error) {
3998
+
console.error('Error fetching data:', error);
3999
+
}
4000
+
}
4001
+
4002
+
fetchData();
4003
+
```
4004
+
4005
+
9. **Event Handling:**
4006
+
4007
+
```javascript
4008
+
let button =document.getElementById('myButton');
4009
+
4010
+
button.addEventListener('click', function() {
4011
+
console.log('Button clicked!');
4012
+
});
4013
+
```
4014
+
4015
+
10. **LocalStorage:**
4016
+
4017
+
```javascript
4018
+
// Save data to local storage
4019
+
localStorage.setItem('username', 'JohnDoe');
4020
+
4021
+
// Retrieve data from local storage
4022
+
let storedUsername =localStorage.getItem('username');
4023
+
console.log('Username:', storedUsername);
4024
+
```
4025
+
4026
+
## Short Hands
4027
+
4028
+
JavaScript offers several shorthand techniques to write code more concisely and improve readability. Here are some common JavaScript shorthand techniques:
4029
+
4030
+
1. **Ternary Operator:**
4031
+
Instead of using an `if-else` statement, you can use the ternary operator for concise conditional expressions.
4032
+
4033
+
```javascript
4034
+
// Long form
4035
+
let result;
4036
+
if (condition) {
4037
+
result ='true';
4038
+
} else {
4039
+
result ='false';
4040
+
}
4041
+
4042
+
// Shorthand
4043
+
let result = condition ?'true':'false';
4044
+
```
4045
+
4046
+
2. **Nullish Coalescing Operator (`??`):**
4047
+
This operator provides a concise way to provide a default value if a variable is null or undefined.
4048
+
4049
+
```javascript
4050
+
// Long form
4051
+
let value;
4052
+
if (value !==null&& value !==undefined) {
4053
+
result = value;
4054
+
} else {
4055
+
result ='default';
4056
+
}
4057
+
4058
+
// Shorthand
4059
+
let result = value ??'default';
4060
+
```
4061
+
4062
+
3. **Arrow Functions:**
4063
+
Arrow functions provide a concise syntax for writing function expressions.
4064
+
4065
+
```javascript
4066
+
// Long form
4067
+
functionadd(x, y) {
4068
+
return x + y;
4069
+
}
4070
+
4071
+
// Shorthand
4072
+
constadd= (x, y) => x + y;
4073
+
```
4074
+
4075
+
4. **Template Literals:**
4076
+
Template literals make it easier to concatenate strings and include variables within strings.
4077
+
4078
+
```javascript
4079
+
// Long form
4080
+
constgreeting='Hello, '+ name +'!';
4081
+
4082
+
// Shorthand
4083
+
constgreeting=`Hello, ${name}!`;
4084
+
```
4085
+
4086
+
5. **Object Property Shorthand:**
4087
+
When creating an object with properties that have the same name as the variables being assigned, you can use shorthand notation.
4088
+
4089
+
```javascript
4090
+
// Long form
4091
+
constname='John';
4092
+
constage=30;
4093
+
constuser= {
4094
+
name: name,
4095
+
age: age
4096
+
};
4097
+
4098
+
// Shorthand
4099
+
constuser= {
4100
+
name,
4101
+
age
4102
+
};
4103
+
```
4104
+
4105
+
6. **Destructuring Assignment:**
4106
+
Destructuring allows you to extract values from arrays or objects and assign them to variables in a concise way.
4107
+
4108
+
```javascript
4109
+
// Long form
4110
+
constperson= { name:'John', age:30 };
4111
+
constname=person.name;
4112
+
constage=person.age;
4113
+
4114
+
// Shorthand
4115
+
const { name, age } = { name:'John', age:30 };
4116
+
```
3896
4117
3897
4118
## List of GitHub Repositories to learn JavaScript
0 commit comments