Welcome 🎉

logo

ReactLMS

Search
Light Mode
Contact Us

4 min to read

Contact us

No results for your search.
Sorry, an unexpected error occurred

What is Javascript?


JavaScript (abbreviated as JS) is a popular and powerful client-side programming language widely used in web development. With the ability to interact with HTML and CSS, JavaScript allows us to create dynamic web pages, web applications, and many other attractive features.


Basic syntax of JavaScript


Before diving into details, let's take a look at the basic syntax of JavaScript:

// Comment trong JavaScript
var x = 5; // Khai báo biến x với giá trị là 5

// In ra console
console.log(x);

// Cấu trúc điều khiển if...else
if (x > 0) {
  console.log("x là số dương");
} else {
  console.log("x là số âm hoặc bằng 0");
}

// Vòng lặp for
for (var i = 0; i < 5; i++) {
  console.log(i);
}

// Hàm trong JavaScript
function add(a, b) {
  return a + b;
}

console.log(add(3, 4)); // Kết quả: 7






Above are some basic syntax in JavaScript. We can declare variables using the keyword 'var', use control statements like 'if...else' and loops like 'for', as well as create functions to perform specific tasks.


Interacting with HTML and CSS


JavaScript has the ability to interact with HTML and CSS, allowing us to change the content and style of web pages. Here is a simple example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Example</title>
  <style>
    .highlight {
      color: red;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h1 id="myHeading">Hello World!</h1>

  <button onclick="changeText()">Đổi nội dung</button>

  <script>
    function changeText() {
      var heading = document.getElementById("myHeading");
      heading.innerHTML = "Xin chào!";
      heading.classList.add("highlight");
    }
  </script>
</body>
</html>






In the example above, we have a <h1> heading and a <button> button. When the button is clicked, the changeText() function is called and it changes the content of the heading to "Hello!" and adds the CSS class "highlight" to make it stand out.


Manipulating events


JavaScript allows us to manipulate events on web pages. We can capture and handle events such as button clicks, drag and drop, input, and much more. Here is an example of handling key press events:

document.addEventListener("keydown", function(event) {
  if (event.key === "Enter") {
    console.log("Bạn đã nhấn phím Enter");
  }
});






In the example above, we use the addEventListener() method to listen for the "keydown" event. In the event handler, we check if the pressed key is "Enter" and display the corresponding message.


Communicating with the server


JavaScript also supports technologies like Fetch API to interact with the server and retrieve data from APIs. Here is an example using Fetch API to retrieve data from an API:

fetch('https://www.googleapis.com/books/v1/volumes?q=frontend')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.log("Đã xảy ra lỗi: " + error);
  });






In the example above, we use the Fetch API to send a GET request to the URL "https://www.googleapis.com/books/v1/volumes?q=frontend". Then, we convert the response to JSON and print the data in the console. If an error occurs, we handle it in the .catch() section.


First Step


After the introduction above, you must have noticed the unique features of Javascript, and to help you learn more in depth, let's start with the following sections:


To learn JavaScript well, we need to understand the most basic knowledge, and to get familiar with the simplest way, let's also refer to the following categories:
Concept In JavaScript programming, a string is an important and common data type. A string represents a sequence of characters, such as a word, a sentence, or a paragraph. Declaration and Creation...
Concept JavaScript provides two basic data types for numbers: number (integer and floating-point) and BigInt (big integer). Integers and floating-point numbers In JavaScript, we can use integers and...
Concept In JavaScript programming, loops are a powerful tool for performing repetitive tasks. We can use loops to execute a block of code multiple times, check conditions, and manipulate elements in...
Concept JSON (JavaScript Object Notation) is a popular data format used for transmitting and storing data. In JavaScript, we can use JSON to represent and work with objects and arrays. Format JSON...
Concept Arrays are an important part of JavaScript programming, allowing you to store and manage multiple values in a single data structure. Declare and initialize an array In JavaScript, we can...
Concept The Spread Operator is represented by three dots .... When used, the Spread Operator helps us "expand" an array or object into individual elements. Therefore, we can pass these...
Concept Date is a built-in object in Javascript used to manage operations related to dates and times. Initialization We can initialize a Date object using the new keyword with the following syntax:...
Concept In web programming, Booleans are an essential part. In Javascript, boolean values are used to represent true or false. Declaration and assignment We can declare and assign boolean values...
Concept Math is a built-in object in Javascript, used to support complex operations during runtime. Basic operations The Math object in Javascript provides methods to perform basic operations such...
Concept Arrow Function is a new feature in Javascript that helps to write shorter and more readable code. Arrow Function provides a concise syntax for declaring functions and mapping parameters and...
Concept Promise is an important and powerful concept for handling asynchronous tasks. Promise helps us create sequential processing chains, handle errors easier, and increase the stability of the...
Concept Callback is a function passed into another function as a parameter. The callback function will be called after completing a certain task. This allows us to control the execution order of...
Concept Regular Expression is a special expression used to search, analyze, and replace character strings in text. This expression is formed from special characters and syntax rules. With Regular...
Concept The Fetch API is a built-in tool in modern browsers like Google Chrome, Firefox, and Edge. It provides an easy and powerful way to communicate with the server and retrieve data from APIs....
Concept The ternary operator allows you to make a choice based on a condition and return a corresponding value. Definition and syntax The ternary operator is an operator that can replace an if-else...
Introduction Optional chaining is a new feature introduced in JavaScript (JS) since version ES2020. This feature simplifies accessing properties or methods of an object that may not exist or be...
Concept Closure is a feature in JavaScript that allows a function to access and use variables from its outer scope, including variables that have escaped the scope of the function. This means that...
Concept Hoisting is the process of moving variable and function declarations to the top of their scope before the code is executed. This means that we can use variables and functions before they are...
Concept Class is an important and powerful concept. It allows you to create objects with unique properties and methods. Using classes helps you organize and reuse code more easily. Initialize class...
Concept In object-oriented programming, the concepts of encapsulation, inheritance, polymorphism, and abstraction are fundamental and important. Object-oriented programming operates with 4...
Concept A cookie is a small piece of data stored on a user's computer through a web browser. Cookies are commonly used to store information such as login information, language settings, personal...
Concept Local storage is an HTML5 API that allows web applications to store data in the user's browser. Data stored in local storage will not be deleted when the user closes the web browser....
Concept Session is a way to store information related to the user's session on the server. When a user accesses a website (opens a new tab), the server creates a new Session and assigns a unique...
Concept The setTimeout function is used to perform an action after a specified amount of time. This is useful when you want to perform a task after a waiting period. How to use The syntax of the...
Concept The setInterval function is used to perform a repeated action after a specified time interval. This is useful when you want to perform a task periodically in your application. Syntax The...



Read more
On This Page