Skip to main content
Engineering LibreTexts

1.14: JavaScript Examples

  • Page ID
    10593
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    600px-Unofficial_JavaScript_logo.svg_.pngOverview

    JavaScript, often abbreviated as JS, is a high-level, interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web. JavaScript enables interactive web pages and therefore is an essential part of web applications. The vast majority of websites use it, and all major web browsers have a dedicated JavaScript engine to execute it.[1]

    JavaScript is one of the most popular current programming languages[2], and is the primary programming language for front-end web development. JavaScript has been implemented in multiple platforms with different I/O commands. Several examples follow.

    Example

    Hello World – Console Log

    // This script displays "Hello world!".
    //
    // References:
    // https://www.digitalocean.com/community/tutorials/how-to-write-your-first-javascript-program
    
    console.log("Hello world!")
    

    Output

    Hello world!
    

    Discussion

    Each code element represents:

    • // begins a comment
    • console.log() writes to the JavaScript console output log
    • "Hello world!" is the literal string to be displayed

    Hello World – Window Alert

    // This script displays "Hello world!".
    //
    // References:
    // https://www.digitalocean.com/community/tutorials/how-to-write-your-first-javascript-program
    
    alert("Hello world!")
    

    Output

    Hello world!
    

    Discussion

    Each code element represents:

    • // begins a comment
    • alert() calls the window alert function to display a message
    • "Hello world!" is the literal string to be displayed

    Hello World – Document Write

    // This script displays "Hello world!".
    //
    // References:
    // https://www.w3schools.com/jsref/met_doc_write.asp
    document.write("Hello world!")
    

    Output

    Hello world!
    

    Discussion

    Each code element represents:

    • // begins a comment
    • document.write() writes output to the current document
    • "Hello world!" is the literal string to be displayed

    JavaScript IDEs

    There are many free cloud-based and local IDEs available to begin coding in JavaScript. Check with your instructor or do your own research for recommendations.

    Local IDEs

    References

    • Wikiversity: Computer Programming

    1. Wikipedia: JavaScript
    2. TIOBE: Index

    1.14: JavaScript Examples is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?