Home > Web Front-end > JS Tutorial > body text

Day f JavaScript

PHPz
Release: 2024-08-01 01:44:13
Original
423 people have browsed it

Day f  JavaScript

Hey curious readers! I'm back with the third tutorial on JavaScript. In this post, we will explore how comments are used and how variables are declared in JavaScript. Let's get started!

1. Comments in JavaScript

What are comments?
Comments are pieces of text in the code that help explain what the code does. They are not executed as part of the program and are meant to enhance code readability.

Types of comments:

  • Single-line comments: These are represented by double slashes //.
// This is a single-line comment
console.log("Hello World"); // This is another single-line comment
Copy after login
  • Multi-line comments: These are used for longer comments that span multiple lines. They are enclosed by /* at the beginning and */ at the end.
/* 
  This is a multi-line comment.
  It can span multiple lines.
*/
console.log("Hello World");
Copy after login

2. Variable Declaration

What are variables?
Variables are symbolic names for values and are used to store data that can be referenced and manipulated in a program.

Ways to declare variables in JavaScript:

  • Using var keyword
  • Using let keyword
  • Using const keyword

Using var keyword:
var is the traditional way to declare variables in JavaScript. Variables declared with var are function-scoped or globally scoped.

function exampleFunction() {
  var name = "Alice";
  console.log(name); // Accessible inside the function
}
exampleFunction();
console.log(name); // Error: name is not defined
Copy after login

If declared outside a function, var is accessible throughout the program.

var name = "Alice";
console.log(name); // Accessible globally
Copy after login

Using let keyword:
Introduced in ECMAScript 2015 (ES6), let allows you to declare block-scoped variables.

{
  let name = "Bob";
  console.log(name); // Accessible within this block
}
console.log(name); // Error: name is not defined
Copy after login

let cannot be redeclared within the same scope.

Using const keyword:
Also introduced in ECMAScript 2015 (ES6), const allows you to declare constants, which are block-scoped like let. The value of a constant cannot be reassigned once it is initialized.

const name = "Charlie";
console.log(name); // "Charlie"
name = "Dave"; // Error: Assignment to constant variable
Copy after login

However, properties of objects or elements of arrays declared with const can still be changed.

const person = { name: "Eve" };
person.name = "Frank"; // Allowed
console.log(person.name); // "Frank"
Copy after login

That's it for this post. I hope you now understand how to use comments and declare variables in JavaScript. In the next post, we will explore data types. Stay connected and don't forget to follow me!

The above is the detailed content of Day f JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template