How do I console.log JavaScript variables as it relates to DOM?

To display variables on console, use document.getElementById(“”).


Following is the code −

 Live Demo

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
   <input type="text" id="printTheData" value="My Name is David">
   const data = document.getElementById("printTheData");

To run the above program, save the file name anyName.html (index.html). Right click on the file and select the option “Open with Live Server” in VS Code editor.


The output is as follows −

Output in the console −