From b39de5f670203fed3dac5b0fbdcb9f5571c008c6 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Tue, 26 Sep 2023 01:28:01 +0600 Subject: [PATCH] wip: add more questions --- .../javascript/content/heap-and-stack.md | 20 +++++++++++++++ .../question-groups/javascript/javascript.md | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/data/question-groups/javascript/content/heap-and-stack.md diff --git a/src/data/question-groups/javascript/content/heap-and-stack.md b/src/data/question-groups/javascript/content/heap-and-stack.md new file mode 100644 index 000000000..bd7ed8581 --- /dev/null +++ b/src/data/question-groups/javascript/content/heap-and-stack.md @@ -0,0 +1,20 @@ +The Head and Stack in JavaScript Engine are two different data structures that store data in different ways. + +## Stack + +The Stack is a small, organized region of memory. It is where primitive values, function calls, and local variables are stored. It follows a "Last In, First Out" (LIFO) order, meaning that the last item added to the stack is the first one to be removed. Each function invocation creates a new stack frame, which contains the function's local variables, return address, and other contextual data. + +## Heap + +The Heap is a large, mostly unstructured region of memory. It is where `objects`, `arrays`, and `functions` are stored. Variables from the Stack (e.g., in functions) point to locations in the Heap for these dynamically allocated structures. + +When you declare a primitive type (like a number or boolean), it's usually managed in the stack. But when you create an object, array, or function, it's stored in the heap, and the stack will hold a reference to that location in the heap. + +For example: + +```js +const name = 'JavaScript'; // Stored on the stack +const roadmap = { name: 'JS' }; // `roadmap` reference on the stack, actual object { name: 'JS' } in the heap +``` + +In the code above, the primitive value `JavaScript` for variable `name` is directly stored on the stack. For the object assigned to `roadmap`, its actual data resides in the heap, and the reference to this data (a memory address pointer) is held on the stack. diff --git a/src/data/question-groups/javascript/javascript.md b/src/data/question-groups/javascript/javascript.md index 150de8035..08fa3ff13 100644 --- a/src/data/question-groups/javascript/javascript.md +++ b/src/data/question-groups/javascript/javascript.md @@ -298,4 +298,29 @@ questions: topics: - 'Core' - 'Advanced' + - question: Is it possible to run JavaScript outside the browser? + answer: Yes, it is possible to run JavaScript outside the browser. There are several ways to run JavaScript outside the browser. You can use **Node.js**, **Deno**, **Bun**, or any other JavaScript runtime environment. + topics: + - 'Core' + - 'Beginner' + - question: Is it possible to run 2 lines of code at the same time in JavaScript? + answer: No, it is not possible to run 2 lines of code at the same time in JavaScript. JavaScript is a single-threaded language, which means that it can only execute one line of code at a time. However, it is possible to run 2 lines of code at the same time using asynchronous code. + topics: + - 'Core' + - 'Beginner' + - question: Is JavaScript a compiled or interpreted language? + answer: JavaScript is an interpreted language. This means that the JavaScript code is not compiled before it is executed. Instead, the JavaScript engine interprets the code at runtime. + topics: + - 'Core' + - 'Beginner' + - question: Are references copied in JavaScript? + answer: No, references are not copied in JavaScript. When you assign an object to a variable, the variable will contain a reference to the object. If you assign the variable to another variable, the second variable will also contain a reference to the object. If you change the object using one of the variables, the change will be visible using the other variable. + topics: + - 'Core' + - 'Intermediate' + - question: What are Heap and Stack in JavaScript? + answer: heap-and-stack.md + topics: + - 'Core' + - 'Advanced' ---