JavaScript Flashcards
(6 cards)
Before Node, what was the only way JavaScript could be executed?
In a browser
What is the relationship between Node.js and JavaScript?
JavaScript is a programming language, while Node.js is a runtime environment that allows JavaScript to run outside of the browser, such as on servers or local machines.
Node.js uses the V8 engine (from Chrome) to execute JavaScript and adds system-level features like file access, networking, and process control that aren’t available in browsers.
👉 JavaScript = language
👉 Node.js = place where it runs outside the browser
ℹ️ What is a runtime environment?
A runtime environment is the system that runs your code — it provides the engine that interprets the language and the tools/libraries needed to interact with the operating system.
If JavaScript is just a language, how is it able to change a web page or access files on your computer? What actually gives it those powers?
JavaScript is just a way for humans to write commands — by itself, it can’t do anything.
What gives JavaScript power is the runtime environment it’s running in:
In the browser, JavaScript runs inside a sandbox using the V8 engine, which gives it access to web page elements like document, window, and fetch() — but not your computer files.
In Node.js, JavaScript also runs on V8, but the runtime gives it access to system tools like fs for files, http for servers, and process for the operating system.
👉 So it’s not JavaScript itself doing the work — it’s the environment deciding what your commands are allowed to influence.
What’s the difference between the JavaScript engine (like V8) and a runtime environment (like the browser or Node.js)? How do they work together?
The JavaScript engine (like V8) is what actually runs your code — it compiles JavaScript into fast machine instructions.
But the engine alone isn’t enough.
To do anything useful — like access files or update a web page — the code needs a runtime environment.
🔧 The Relationship:
JavaScript = the human-written instructions
V8 engine = the car engine — it makes the instructions run
Runtime environment = the roads, rules, and destinations — it defines what the code is allowed to interact with.
Examples:
In the browser, JavaScript runs using the V8 engine and is allowed to interact with things like the web page (HTML/CSS), user events (like clicks), and make network requests.
In Node.js, JavaScript also runs using the V8 engine, but instead of web page access, it’s allowed to interact with your computer’s file system, create servers, and use operating system features.
✅ So the engine runs the code,
and the runtime environment determines the code’s powers.
How does a JavaScript sandbox actually work? What stops compiled code from accessing memory or files it shouldn’t?
A sandbox is a controlled environment where code can only access specific, safe resources. In JavaScript, the sandbox is enforced at multiple levels to prevent code from stepping outside its allowed boundaries.
Here’s how it works:
🔐 1. Process Isolation (OS-level)
Each browser tab or Node.js program runs in its own process.
The operating system and CPU enforce memory separation — code in one process can’t touch another’s memory.
🛠 2. No Dangerous APIs (Runtime-level)
The browser’s JavaScript runtime only exposes safe APIs like document, fetch, etc.
It doesn’t provide access to the file system, OS commands, or raw memory tools.
🧠 3. V8 Engine Memory Control (Engine-level)
V8 compiles JavaScript into machine code, but:
It runs the code inside a controlled heap
It does not allow direct memory access
It manages memory and prevents out-of-bounds operations
🚫 4. No Syscalls or Unsafe Instructions
Even compiled machine code from JavaScript can’t trigger dangerous system calls, because:
Those APIs don’t exist in the browser
The OS would block unauthorized system access
✅ TL;DR:
JavaScript is sandboxed by the browser runtime, V8 engine, and OS protections.
Even after being compiled to machine code, it’s restricted to only what the environment allows — nothing more.
What are all the common methods and properties on DOM elements that can be accessed or modified through JavaScript?
DOM elements have a wide range of properties (data you can read/write) and methods (actions you can perform). Here are the most important ones:
🔹 Content and Attributes
textContent – get/set text content
innerHTML – get/set HTML inside element
outerHTML – get/set HTML including the element itself
innerText – similar to textContent, but respects CSS
value – input/textarea value
id, className, name – get/set attributes
getAttribute(), setAttribute(), removeAttribute()
hasAttribute()
🔹 Classes and Styling
classList.add(), remove(), toggle(), contains()
style.propertyName – inline styles
style.cssText – full inline style string
computedStyle = getComputedStyle(element) – read applied styles
🔹 Traversing / Selecting
parentElement, children, childNodes
firstElementChild, lastElementChild
nextElementSibling, previousElementSibling
querySelector(), querySelectorAll()
closest()
🔹 Manipulation
appendChild(), removeChild()
replaceChild()
insertBefore()
append(), prepend(), remove()
cloneNode(true/false)
🔹 Events
addEventListener(‘click’, fn)
removeEventListener(‘click’, fn)
dispatchEvent(new Event(‘custom’))
🔹 Visibility and State
hidden, disabled, checked, selected, readonly
focus(), blur()
scrollIntoView()
🧠 These methods and properties allow you to read, change, move, style, or interact with any element in the page — all through JavaScript via the DOM.