JavaScript Summary
JavaScript
is an essential programming language in web development, often referred to as the brain of web pages.
Let's quickly summarize the key concepts of JavaScript.
1. Variables
Containers for storing data. Types of data include numbers, strings, booleans, objects, and more.
Declare a reassignable variable using let
let userName = 'CodeCompanion';
2. Functions
Reusable blocks of code that perform specific tasks.
Declaration of the greet function
function greet(userName) {
console.log('Hello, ' + userName + '!');
}
greet('CodeCompanion'); // Outputs: "Hello, CodeCompanion!"
3. Conditional Statements
Execute different code based on certain conditions.
if-else conditional statement
if (userName === 'CodeCompanion') {
console.log('Hello, CodeCompanion!');
} else {
console.log('Hello, Guest!');
}
4. Loops
Repeat a block of code multiple times under certain conditions.
for loop
for (let i = 0; i < 5; i++) {
console.log(i); // Outputs numbers from 0 to 4
}
5. Objects
Collections of data in the form of key-value pairs, where the key is referred to as a property.
person object
const person = {
name: 'CodeCompanion', // name property, string value
age: 25, // age property, number value
greet: function () { // greet property, function value
console.log('Hello, ' + this.name + '!');
},
};
person.greet(); // Outputs: "Hello, CodeCompanion!"
6. Events
User interactions with a web page, such as button clicks, form submissions, and page loads.
Detecting a Button Click Event
let button = document.querySelector('button');
// Button click event listener
button.addEventListener('click', function () {
alert("You've clicked the button!");
});
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.