"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Day f JavaScript

Day f JavaScript

Published on 2024-08-07
Browse:276

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
  • 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");

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

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

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

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

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

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"

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!

Release Statement This article is reproduced at: https://dev.to/shoyab1707/day-3-of-30-javascript-51ak?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3