Javascript is a high-level, dynamically typed programming language commonly used for client-side web development and server-side programming with Node.js.
Javascript has dynamic typing, meaning that a variable's type can change at runtime. Variables can be declared using the keywords var, let, or const.
var x;
let y;
const z = 10;var is function scoped, while let and const are block scoped. const is used to declare a constant value that cannot be re-assigned.
Javascript has the following primitive data types:
- String: a sequence of characters, denoted with quotes
''or"". - Number: numeric values, including integers and floating point numbers.
- Boolean: either
trueorfalse. - Undefined: a variable that has been declared, but has no value.
- Null: explicitly represents the absence of any object value.
| Data Type | Description | Size (in bytes) |
|---|---|---|
| Number | Represents numeric values, including integers and floating-point numbers | 8 |
| String | Represents textual data | The size of a string is proportional to the length of the string, typically 2 bytes per character |
| Boolean | Represents a logical value | 1 |
| Undefined | Represents an undefined value | 0 |
| Null | Represents a null value | 0 |
| Object | Represents a collection of properties and methods | Varies |
| Symbol | Represents a unique identifier | Varies |
Arrays are ordered collections of values that can hold any data type. They are declared with square brackets [].
let numbers = [1, 2, 3];
let mixed = [1, "two", false];Objects are collections of key-value pairs. They are declared with curly braces {}.
let person = {
name: "John Doe",
age: 30,
occupation: "developer",
};Javascript has various operators for performing arithmetic, assignment, comparison, and logical operations.
+: addition-: subtraction*: multiplication/: division%: modulus (remainder after division)
=: assignment+=: addition and assignment-=: subtraction and assignment*=: multiplication and assignment/=: division and assignment
==: equality===: strict equality (value and type)!=: inequality!==: strict inequality (value and type)>: greater than<: less than>=: greater than or equal to<=: less than or equal to?: ternary operator
&&: logical AND||: logical OR!: logical NOT
typeof: returns the type of a variableinstanceof: returns true if object is an instance of an object type
Conditional statements are used to make decisions in code based on whether a condition is true or false.
if (condition) {
// code to be executed if condition is true
} else if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if all conditions are false
}Loops are used to repeatedly execute a block of code.
A for loop is used to execute a block of code a specified number of times.
for (let i = 0; i < 10; i++) {
console.log(i);
}A for-in loop is used to iterate over the properties of an object.
let person = {
name: "John Doe",
age: 30,
};
for (let prop in person) {
console.log(prop + ": " + person[prop]);
}A for-of loop is used to iterate over the values of an iterable object, such as an array.
let colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}A while loop is used to execute a block of code as long as a specified condition is true.
let i = 0;
while (i < 10) {
console.log(i);
i++;
}A do-while loop is similar to a while loop, but the block of code will always be executed at least once.
let i = 0;
do {
console.log(i);
i++;
} while (i < 10);-
Mozilla Developer Network (MDN): MDN is an excellent resource for learning more about JavaScript. It includes detailed documentation on the language itself, as well as tutorials and guides on specific topics.
-
JavaScript.info: JavaScript.info is a comprehensive guide to JavaScript that covers the language from the basics to advanced topics. It includes interactive exercises and examples to help you learn and practice.
-
W3Schools: W3Schools is a popular website for learning web development, including JavaScript. It includes tutorials and examples on a wide range of topics, from basic syntax to advanced techniques.
-
Stack Overflow: Stack Overflow is a community-driven Q&A site for programmers. It includes a wealth of information on JavaScript, from common questions to advanced topics.
-
GitHub: GitHub is a popular code hosting platform where you can find a wide range of open source JavaScript projects. It's a great place to learn from other developers and contribute to the community.