JavaScript Basics: Outputting Data and Pop-up Boxes

Kaynak
en-orig
Oct 27, 2026 Oct 27, 2026
Video preview
Paylaş:

This article covers practical JavaScript concepts for displaying data, including variables, data types, and methods like document.write, innerHTML, alert, confirm, prompt, and console.log.

Variables, Data Types, and document.write ⏱ 1:31

  • •Data types define the kind of data stored, such as numbers, strings, dates, or other types.
  • •A string is a data type where data is stored as text, which can include characters, numbers, and symbols.
  • •Create a variable using the let keyword, followed by a name and a value, e.g., let a = 2;.
  • •To display output, use document.write() with the variable or expression inside the parentheses, e.g., document.write(c);.
  • •Strings must be enclosed in double quotes; without quotes, the browser treats the word as an undefined variable and displays nothing.
  • •Arithmetic operations work on numbers, not strings: 2 + 3 yields 5, while "5" + "4" yields "54".
  • Modifying HTML with innerHTML ⏱ 11:04

  • •innerHTML allows you to assign content to a specific HTML element identified by an ID.
  • •Steps: create an HTML element with an id attribute (e.g., <p id="A"></p>), then in JavaScript use document.getElementById('A').innerHTML = 'New content';.
  • •The E in getElementById must be capitalized, the B in ById must be capitalized, and the H in innerHTML must be capitalized; otherwise, the code will not work.
  • •This method replaces the element's content with the specified text or HTML.
  • Pop-up Boxes: alert, confirm, and prompt ⏱ 13:37

  • •alert() displays a message in a pop-up box; it can be called as window.alert() or simply alert().
  • •Example: alert('Welcome to our website'); shows a pop-up with that text.
  • •confirm() displays a pop-up with OK and Cancel buttons, returning true for OK and false for Cancel.
  • •Example: confirm('Please confirm it'); shows the message.
  • •prompt() displays a pop-up with a text input field, allowing the user to enter a value.
  • •Example: prompt('Please confirm it'); shows the message and an input box.
  • Using the Console ⏱ 15:15

  • •console.log() outputs messages to the browser's console, not to the web page itself.
  • •Access the console by right-clicking on the page, selecting "Inspect", and clicking the "Console" tab.
  • •Example: console.log('hello'); prints hello and undefined (the return value of console.log).
  • •Variables can be logged: let a = 2; console.log(a); prints 2.
  • •The console is useful for debugging and writing/testing code.
  • Key Takeaways

  • •Variables are declared with let and can store various data types like numbers and strings.
  • •document.write() outputs data to the web page, while console.log() outputs to the browser console.
  • •Strings must be enclosed in quotes; numbers without quotes are treated as numeric values and can be used in calculations.
  • •innerHTML modifies the content of an HTML element selected by its ID.
  • •alert, confirm, and prompt create pop-up boxes for messages, confirmations, and user input, respectively.
  • Conclusion

    JavaScript provides multiple ways to output data and interact with users through pop-up boxes and the console. Understanding data types and variables is essential for effective programming.

    Canlı web aramasıyla doğrulanır — iddialar tahmin edilmez, gerçek kaynaklarla karşılaştırılır.

    Bu video hakkında soru sor

    Görsel Öne Çıkanlarbeta