Top Stories

Ankaj Gupta
January 11, 2019

How To Write Comments in JavaScript

Comments in JavaScript

JavaScript comments describe and explain code and improve readability. A comment is a statement that is not executed. The JavaScript interpreter skips comments, so they don't affect runtime.

Why Use Comments?

Comments add details, warnings, and suggestions so users and other developers can understand the code.

There are 3 types of comments in JavaScript:

  • Single-Line Comment
  • Multi-Line Comment
  • ScriptDoc Comment

1. Single-Line Comment

Single-line comments use double forward slashes // and can appear before any statement.

<script>
    // Write on browser
    document.write("Hello Javascript!");
    
    // Write text in <h2> Heading
    document.write("<h2> Hello Javascript! </h2>");  
</script>

Output:

Hello Javascript!

Hello Javascript!

2. Multi-Line Comment

Multi-line comments are used for longer descriptions. They start with /* and end with */.

Note: Multi-line comments can also be used for single-line comments if you prefer.

<script>
    /*
        First line: Write Simple write
        Second line: Write text in <h2> Heading
    */
    document.write("Hello World!");
    document.write("<h2> Hello World! </h2>");  
</script>

Output:

Hello World!

Hello World!

3. ScriptDoc Comment / JavaScript Documentation

ScriptDoc (JSDoc) is a JavaScript documentation technique for documenting functions, parameters, return values, and more.

Best Practice: Use JSDoc comments to document functions, arguments, and return types for better code maintainability.

Syntax:

<script>
    /** 
     * ScriptDoc technique to write comment
     * @TagName Description
     * @author   
     * @version 
     * ….
    **/ 
</script>

Complete Example:

<script>
    /** 
     * Multiplication of two numbers 
     * @param {Number} a - First number
     * @param {Number} b - Second number
     * @return {String} mult - Result of multiplication
    **/ 
    function multiplication(a, b) { 
        mult = a * b;
        return "Output is : " + mult.toString(); 
    }
    
    result = multiplication(7, 10);
    document.write(result);
</script>

Output:

Output is : 70

ScriptDoc Tags Reference

Common JSDoc tags for documenting JavaScript:

Tag Description
@author Author of JavaScript file, functions, class
@classDescription Brief description of the Class
@constructor Specifies this function is a constructor
@example Describe a real example for how to use this function
@method Specifies the method name in class
@param Specifies parameter of this function
@private Indicates that a class or function is private
@property Indicates specified property are instance of the class
@return Specifies the return values of a function
@type Specify the data type of this property
@version Specify the version number of file

Comment Types Comparison

Comment Type Syntax Best For
Single-Line // comment Quick notes, inline explanations
Multi-Line /* comment */ Longer explanations, disabling code
JSDoc /** */ Function documentation, API generation

Best Practices

✅ Do's

  • • Document complex logic
  • • Explain "why", not just "what"
  • • Use JSDoc for functions
  • • Keep comments up-to-date

❌ Don'ts

  • • Don't state the obvious
  • • Avoid outdated comments
  • • Don't over-comment simple code
  • • Remove commented-out code

Summary

JavaScript comments improve code readability. Use single-line comments (//) for short notes, multi-line comments (/* */) for longer descriptions, and JSDoc comments (/** */) for function documentation.

Good commenting practices improve maintainability and help other developers understand your code.

JavaScript web development and designing
Read
Ankaj Gupta
January 05, 2019

how to use javascript file in html

How to Use JavaScript in HTML

To write JavaScript code, you need a basic text editor like Notepad (Windows), Gedit (Linux), or use advanced editors like VS Code, Brackets, or Atom that feature syntax highlighting. JavaScript files are identified by the .js file extension.

JavaScript Integration Basics

JavaScript code is written between <script> and </script> tags. These tags mark where JavaScript starts and ends in your HTML page.

Note: You can specify the language with <script type="text/javascript">. However, from HTML5, the type attribute is optional as JavaScript is the default scripting language in HTML.

Method 1: Embedded JavaScript

JavaScript code directly embedded into the HTML page using <script> tags

Method 2: External JavaScript

JavaScript code in separate .js file referenced via src attribute

JavaScript Placement in HTML

There are 3 places to put JavaScript code in an HTML page:

1. Between <head> and </head> Tags

Scripts to be executed when called or when an event is triggered go in the head section. This ensures the script is loaded before use:

<html>
<head>
    <script>
        // Write your JavaScript code here
    </script>
</head>
<body>
    // Write your HTML code here
</body>
</html>

2. Between <body> and </body> Tags

Scripts to be executed when the page loads go in the body section, generating page content:

<html>
<head>
</head>
<body>
    <script>
        // Write your JavaScript code here
    </script>
</body>
</html>

3. JavaScript in Both <head> and </body>

You can place an unlimited number of <script> tags in your document:

<html>
<head>
    <script>
        // JavaScript for initialization
    </script>
</head>
<body>
    <script>
        // JavaScript for content generation
    </script>
</body>
</html>

Using External JavaScript Files

Sometimes you might want to run the same JavaScript code on several pages without rewriting the same script. Create an external JavaScript file to simplify this:

  • • JavaScript external files are saved with a .js extension
  • • Create a .js file and place it in a JavaScript subfolder (good practice)
  • • Link the file using the src attribute of the <script> element
  • • Use full URL or path relative to the current web page

Create External JavaScript File

Create a file named myScriptFile.js:

document.write("Hello JavaScript!");

Link External JavaScript File

Example 1: Same Folder

Links to a script in the same folder as the current page:

<script src="myScriptFile.js"></script>

Example 2: Specified Folder

Uses a script located in a specified folder on the website:

<script src="/JavaScript/myScriptFile.js"></script>

Example 3: Full URL

Uses a full URL to link to a script:

<script src="https://tutorials.coderwebsite.com/JavaScript/myScriptFile.js"></script>

Important Notes

  • No <script> Tags: External JavaScript files should NOT contain the <script> tag
  • Placement Flexibility: External JavaScript files can be added to either the <body> or </head> tag
  • File Organization: Keep external .js files in a dedicated folder like /js or /JavaScript
  • Loading Order: Scripts in <head> load before page content; scripts in <body> can load while page renders

Best Practices

✅ Do's

  • • Use external files for reusable code
  • • Place scripts at the bottom of <body> for better performance
  • • Use async or defer attributes for external scripts
  • • Organize JavaScript files in folders

❌ Don'ts

  • • Don't include <script> tags in .js files
  • • Avoid inline JavaScript in production
  • • Don't mix external and inline scripts excessively
  • • Avoid blocking scripts in <head>

When to Use Each Method

Method When to Use Best For
Inline in <head> Global config, early initialization Site-wide settings
Inline in <body> Page-specific code Quick prototypes, small functions
External File Reusable code across pages Production applications

Summary

JavaScript can be integrated into HTML in multiple ways: embedded in <head>, in <body>, or as external files. For production applications, external JavaScript files are recommended for better code organization, reusability, and maintainability.

Choose the method that best fits your needs: inline scripts for quick tests and small functions, external files for production-ready code that needs to be reused across multiple pages.

JavaScript web development and designing
Read
Ankaj Gupta
December 25, 2018

How node.js is different from PHP and ASP ?

Node.js is different from PHP and ASP

A quick look at how request handling differs between Node.js (event-driven, non‑blocking) and traditional platforms like PHP/ASP (threaded, blocking).

N

How Node.js handles requests

  • •

    Node.js sends the request to the server.

  • •

    Picks new requests without waiting for the current one to finish (non‑blocking).

  • •

    When a request completes, data is returned to the client via callbacks/promises.

  • •

    Eliminates idle waiting; continues processing the next request immediately.

P

How PHP/ASP handle requests

  • •

    Sends a request to the server.

  • •

    Waits until the request is completed (blocking per request/thread).

  • •

    Returns data only after completion.

  • •

    Then sends the next request.

Key differences

  • •

    Concurrency model: Node.js is event‑loop based; PHP/ASP rely on threads/process per request.

  • •

    I/O strategy: Node.js non‑blocking I/O vs. typical blocking I/O stacks.

  • •

    Resource usage: Node.js reuses a single event loop; PHP/ASP generally allocate per request.

  • •

    Use cases: Node.js excels at real‑time, streaming, and chat; PHP/ASP suit request/response MVC apps.

Tip: In Node.js, prefer async/await with Promises to keep non‑blocking code readable and maintainable.

Node.js web development and designing
Read