An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
JavaScript Basic Introduction!
Here is a quick reference cheat sheet for JavaScript, including common syntax, methods, and operators:
Syntax
Variables
To declare a variable in JavaScript, use the var keyword followed by the name of the variable:
Copy code
var myVariable;
You can also assign a value to the variable at the same time:
Copy code
var myVariable = 'hello';
In modern versions of JavaScript, you can also use the let and const keywords to declare variables. let is similar to var, but the variable can only be re-assigned within the same block of code. const is used for variables that cannot be re-assigned or redeclared.
Functions
To define a function in JavaScript, use the function keyword followed by the name of the function and a set of parentheses:
function myFunction() {
// function code goes here
}
You can also include parameters in the parentheses:
function myFunction(param1, param2) {
// function code goes here
}
To call a function, simply use its name followed by a set of parentheses:
myFunction();
Objects
To create an object in JavaScript, use the {} notation:
var myObject = {};
You can add properties to the object using the key: value syntax:
var myObject = {
key1: 'value1',
key2: 'value2'
};
You can access object properties using the dot notation:
myObject.key1; // returns 'value1'or using the square bracket notation:myObject['key1']; // returns 'value1'Methodsconsole.log()
The console.log() method is used to print messages to the console for debugging purposes.
console.log('Hello, world!');document.getElementById()
The document.getElementById() method is used to get a reference to an element on the page by its ID.
var element = document.getElementById('my-element');array.push()
The push() method is used to add an element to the end of an array.
var arr = [1, 2, 3];arr.push(4); // arr is now [1, 2, 3, 4]string.toUpperCase()
The toUpperCase() method is used to convert a string to uppercase.
var str = 'hello';str.toUpperCase(); // returns 'HELLO'
Operators
Arithmetic operators
+: addition
-: subtraction
*: multiplication
/: division
%: modulus (remainder)
Comparison operators
==: equal to
!=: not equal to
: greater than
<: less
It will only help you get to know how it is similar to other programming Languages. You will have to learn more through online resources to get a grip.
All the Best!
Original Link: https://dev.to/darkxenium/javascript-basic-introduction-3na1
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To