JavaScript


This draft deletes the entire topic.

expand all collapse all

Examples

  • 189

    Nearly all web browsers and JavaScript environments support writing messages to a console using a suite of logging methods. The most common of these is the console.log() method.

    Open up the JavaScript Console in your browser, type the following, and press Enter:

    console.log("Hello, World!");
    

    Console Log Output in Google Chrome

    In this example the console.log() function prints Hello, World! to the console and returns undefined (which is also shown above in the console output window), because it has no explicit return value.

    The console.log() function is also commonly used for debugging purposes. See the Console topic for more instructions on using the console.

    Note: It is also important to note that console.log() can also be used to log variables, and not only strings. This is especially useful when logging objects. For example:

    var foo = "bar";
    console.log(foo);
    // Prints "bar" to the console, returns undefined as in the example above.
    

    console.log() can be used with variables

    Logging an object and all of its properties and methods (this is useful, for example, to log JSON responses received from an API call):

    enter image description here

    For more information on how to use the console, see the Console topic.

  • 51

    DOM stands for Document Object Model and it is an object-oriented approximation from structured documents like XML and HTML.

    Setting the textContent property of an Element is one way to output text on a web page.

    document.getElementById("paragraph").textContent = "Hello, World";
    
    Associated HTML:
    <p id="paragraph"></p>
    

    This will set the text content of the element that has the id paragraph to "Hello, World".

    View Demo

    Or, if you haven't yet defined the HTML element in your HTML page, you can create a new HTML element programmatically. In this example we are creating a new <p></p>:

    const element = document.createElement('p');
    element.textContent = "Hello, World";
    document.body.appendChild(element); //add the newly created element to the DOM
    

    If you're trying to manipulate elements (the DOM) using JavaScript, the JavaScript code must be run after the relevant element has been created in the document. This often means putting the JavaScript <script> tags after all of your other <body> content, or using event such as onload or jQuery's $(document).ready() to delay your code until after the page has loaded to execute the code once the document is ready.

  • 23

    The alert method displays an alert box. It takes a parameter which is displayed to the user:

    alert('hello, world!');
    

    In Chrome, you would get a pop-up like this: alert result

    Note: The alert method is technically a property of window object, but since all window properties are automatically global variables, we can use alert as a global variable instead of as a property of window, simply alert() instead of window.alert().

    Unlike using console.log, alert acts as a modal prompt meaning that no other JavaScript code will execute until the alert is dismissed:

    alert('Pause!');
    console.log('Alert was dismissed');
    

    More information about usage of the alert method can be found in the modals prompts topic.

    The use of alerts is usually discourage in favour of other methods that do not block user from interacting with the page, in order to create a better user experience. Nevertheless, it can be useful for debugging.

Please consider making a request to improve this example.

Remarks

JavaScript (not to be confused with Java) is a dynamic, weakly-typed language used for client-side as well as server-side scripting.

JavaScript is a case-sensitive language. It means the language considers capital letters as different from their lowercase counterparts. Keywords in JavaScript are all lowercase.

JavaScript is a commonly referenced implementation of ECMAScript standard.

Topics in this tag often refer to the use of JavaScript within the browser, unless otherwise stated. JavaScript files alone can't be run directly by the browser; it's necessary to embed them in an HTML document. If you have some JavaScript code you'd like to try, you can embed it in some placeholder content like this, and save the result as example.html:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test page</title>
  </head>
  <body>
    Inline script (option 1):
    <script>
      // YOUR CODE HERE
    </script>
    External script (option 2):
    <script src="your-code-file.js"></script>
  </body>
</html>

Versions

VersionRelease Date
11997-06-01
21998-06-01
31998-12-01
E4X2004-06-01
52009-12-01
5.12011-06-01
62015-06-01
72016-06-14
82017-07-01
Still have a question about Getting started with JavaScript? Ask Question

Topic Outline