DeveloperBreeze

Hello World and Comments

Chapter 2: Basics of JavaScript

Section 2.1: Syntax and Basics

Tutorial 2.1.1: Hello World and Comments


Hello World and Comments in JavaScript

The first step in learning JavaScript is to write a simple program that outputs "Hello, World!" to the console. Additionally, understanding how to use comments is essential for writing clean, maintainable code.


Writing Your First JavaScript Program

  1. Creating a Script:
  • Open a code editor (e.g., VS Code) or a browser console.
  1. The Code:
  • In your script file or console, type:
     console.log("Hello, World!");
  • What happens?
  • The console.log() function outputs text to the console.
  • "Hello, World!" is a string (text) enclosed in double quotes.
  1. Running the Code:
  • In a browser:
  • Open the console (Ctrl+Shift+J or Cmd+Option+J) and type the code.
  • In Node.js:
  • Save the code in a file (e.g., hello.js) and run it using:
       node hello.js
  • The output will be:
     Hello, World!

Comments in JavaScript

Comments are notes in the code that are ignored by the JavaScript engine. They are useful for explaining what the code does or for temporarily disabling parts of the code.

  1. Single-Line Comments:
  • Begin with //.
  • Example:
     // This is a single-line comment
     console.log("Hello, World!"); // Outputting to the console
  1. Multi-Line Comments:
  • Enclosed within /<em> </em>/.
  • Example:
     /*
      This is a multi-line comment.
      It spans multiple lines.
     */
     console.log("Hello, World!");
  1. Uses of Comments:
  • Documenting code logic.
  • Temporarily disabling code during debugging.
  • Adding notes or reminders for developers.

Common Errors to Avoid

  1. Missing Quotes Around Strings:
  • Incorrect:
     console.log(Hello, World!);
  • Correct:
     console.log("Hello, World!");
  1. Unmatched Parentheses:
  • Incorrect:
     console.log("Hello, World!";
  • Correct:
     console.log("Hello, World!");

Exercise

  1. Write a script that outputs your name to the console.
  • Example:
     console.log("My name is Alice.");
  1. Add comments to explain each line of your script.

Related Posts

More content you might like

Tutorial

ما هو حقن التبعيات (Dependency Injection)؟

  • هيكلة واضحة وسهلة الفهم.
  • تقليل التكرار وزيادة إعادة الاستخدام.
  • مرونة عالية في تبديل المكونات.
  • دعم أفضل لاختبارات الوحدة (Unit Testing).
  • فصل مسؤوليات إنشاء التبعيات عن مسؤوليات العمل الفعلي.

حقن التبعيات ليس مجرد تقنية، بل هو نمط يُسهم في بناء أنظمة نظيفة، قابلة للتوسّع، وسهلة الصيانة. اعتماد هذا الأسلوب يرفع من جودة العمل، ويمنح المطور قدرة أكبر على التحكم في هيكلة التطبيق، خاصةً في الأنظمة الكبيرة أو المعتمدة على خدمات متعددة.

Dec 01, 2025
Read More
Tutorial
javascript

Comparison and Logical Operators

Logical operators combine multiple conditions or values.

// AND operator
console.log(true && true); // true
console.log(true && false); // false

// OR operator
console.log(false || true); // true
console.log(false || false); // false

// NOT operator
console.log(!true); // false
console.log(!false); // true

Dec 11, 2024
Read More
Tutorial
javascript

Arithmetic Operators

  • Subtracts the second number from the first.
  • Example:
     let difference = 10 - 5; // 5

Dec 11, 2024
Read More
Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

In JavaScript, non-primitive data types, such as objects, arrays, and functions, allow for more complex data structures and behaviors. Unlike primitives, non-primitive types are mutable and can hold multiple values or properties.

Objects are key-value pairs that can represent more complex data.

Dec 11, 2024
Read More

Discussion 2

Please sign in to join the discussion.

Y

yussf

Feb 11, 2025 at 01:50 AM

lllllllllllll

Y

yussf

Feb 11, 2025 at 01:51 AM

lllllllllllll