This content originally appeared on DEV Community 👩💻👨💻 and was authored by Avinash Kumar
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'
Methods
console.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!
This content originally appeared on DEV Community 👩💻👨💻 and was authored by Avinash Kumar
Avinash Kumar | Sciencx (2022-12-29T05:27:27+00:00) JavaScript Basic Introduction!. Retrieved from https://www.scien.cx/2022/12/29/javascript-basic-introduction/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.