CamKode

JavaScript Fundamentals: Understanding the Basics

Avatar of Kosal Ang

Kosal Ang

Sun Jan 07 2024

JavaScript Fundamentals: Understanding the Basics

1. Introduction to JavaScript

JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.

1console.log("Hello, JavaScript!"); // Outputs text to the browser console
2
basic console

2. Data Types and Variables

JavaScript supports various data types like strings, numbers, booleans, arrays, and objects. Variables are declared using let, const, or var.

1let name = "Kosal"; // String variable
2let age = 25; // Number variable
3const isStudent = true; // Boolean variable
4

3. Operators and Expressions

JavaScript offers arithmetic, comparison, and logical operators to perform calculations and comparisons.

1let result = 10 + 5; // Addition operator
2let isEqual = 20 === 20; // Equality operator
3let isValid = (age >= 18) && (isStudent === false); // Logical AND operator
4

4. Control Structures: Conditionals and Loops

Control the flow of execution using if-else statements and loops.

1if (age >= 18) {
2  console.log("You are an adult.");
3} else {
4  console.log("You are a minor.");
5}
6
7for (let i = 0; i < 5; i++) {
8  console.log(`Count: ${i}`);
9}
10
basic console

5. Functions and Scope

Functions encapsulate blocks of code and can take parameters and return values.

1function greet(name) {
2  return `Hello, ${name}!`;
3}
4
5console.log(greet("Kosal")); // Outputs: Hello, Kosal!
6
basic console

6. Arrays and Objects

JavaScript arrays and objects allow storing collections of data and key-value pairs.

1let numbers = [1, 2, 3, 4, 5]; // Array
2let person = { name: "Kosal", age: 25, isStudent: true }; // Object
3
4console.log(numbers[2]); // Accessing array element
5console.log(person.name); // Accessing object property
6
basic console

These examples illustrate foundational JavaScript concepts, demonstrating how variables, operators, control structures, functions, arrays, and objects are used within JavaScript code.

Related Posts

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML

Asynchronous JavaScript: Promises and Async/Await Explained

Asynchronous JavaScript: Promises and Async/Await Explained

JavaScript uses asynchronous programming. Promises and `async`/`await` are two powerful features that facilitate asynchronous operations in JavaScript

Understanding Closures and Scope in JavaScript

Understanding Closures and Scope in JavaScript

JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves

Creating a Responsive Sidebar with Icons and Expandable Menus

Creating a Responsive Sidebar with Icons and Expandable Menus

If you're looking to build a responsive sidebar with icons and expandable menus, this HTML and CSS template can serve as a great starting point

Working with Arrays and Objects in JavaScript

Working with Arrays and Objects in JavaScript

Arrays and objects in JavaScript is fundamental for managing and manipulating data

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript

Get Monday and Sunday of the Week in JavaScript

Get Monday and Sunday of the Week in JavaScript

To get the Monday and Sunday of the week, use the setDate() method to set the day of the month of a date to the Monday and Sunday

Find all Permutations in JavaScript

Find all Permutations in JavaScript

Generate all permutations of string and filter unique string by using JavaScript

Close Dropdown Menu on Click Outside

Close Dropdown Menu on Click Outside

Dropdowns are a feature common to many websites. It's very useful, as they make it easy to show additional data only when it is needed.

How to install Tailwind CSS in Next.js

How to install Tailwind CSS in Next.js

TailwindCSS is a popular CSS framework that can help you create nice user interfaces quickly with pre-made CSS classes.

© 2024 CamKode. All rights reserved

FacebookTwitterYouTube