javascript basics
javascript basics
Answer:
JavaScript supports eight data types.
Primitive types (immutable):
String
Number
Boolean
undefined
null
BigInt
Symbol
Answer:
undefined means a variable has been declared but has not been assigned a value.
Example:
js
CopyEdit
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
Answer:
The type of NaN is "number".
This is because NaN is a special value representing an invalid number, like the result of 0 / 0.
js
CopyEdit
Answer:
The typeof operator returns a string indicating the type of the operand. It's commonly used for type checking.
Examples:
js
CopyEdit
Answer:
typeof null returns "object" due to a bug in the language’s implementation that dates back to the first version of
JavaScript. Although null is a primitive type, typeof mistakenly identifies it as "object".
Answer:
Primitive types are immutable and hold single values (e.g., string, number, boolean, etc.).
Non-primitive types (objects) are mutable and can hold multiple values or collections.
Answer:
JavaScript is a dynamically typed language, meaning variables can hold values of any type without declaring the type
explicitly.
js
CopyEdit
Answer:
Falsy values are values that evaluate to false in a boolean context. They include:
false
0
-0
null
undefined
NaN
Answer:
Use Array.isArray() which returns true if the variable is an array:
js
CopyEdit
Array.isArray({}); // false
Answer:
JavaScript performs implicit type conversion (coercion) in certain operations:
js
CopyEdit
Answer:
== (loose equality): compares values after type coercion.
js
CopyEdit
'5' == 5; // true
Answer:
js
CopyEdit
Answer:
Yes. JavaScript allows variables to change their type dynamically.
js
CopyEdit
Answer:
Symbol is a primitive data type introduced in ES6 to create unique identifiers for object properties.
js
CopyEdit
let id = Symbol('id');
let user = {
[id]: 123
};
Each symbol is guaranteed to be unique.
🔹 15. What’s the difference between Object, Array, and Function types?
Answer:
js
CopyEdit
Would you like me to prep some advanced-level or practical scenario questions on data types next?
4o
You said:
ChatGPT said:
Great question!
🔹 What is immutable?
✅ Example (immutable):
js
CopyEdit
str[0] = "H";
Strings in JavaScript are immutable. Even if you try to change a character, the original string stays the same.
🔹 What is mutable?
✅ Example (mutable):
js
CopyEdit
arr[0] = 99;
console.log(arr); // [99, 2, 3]
Answer:
A variable in JavaScript is a named container used to store data that can be changed later. It allows us to reference and
manipulate values in a program.
js
CopyEdit
Variables can hold any data type — strings, numbers, booleans, objects, etc.
Answer:
JavaScript provides three keywords to declare variables:
js
CopyEdit
var x = 10;
let y = 20;
const z = 30;
Hoisting Yes (undefined) Yes (but not initialized) Yes (but not initialized)
Example:
js
CopyEdit
function test() {
if (true) {
let b = 2; // block-scoped
const c = 3; // block-scoped
console.log(a); // 1
console.log(b); // Error
Answer:
Hoisting is JavaScript's default behavior of moving declarations to the top of the scope before code execution.
let and const are hoisted but not initialized — accessing them before declaration causes a ReferenceError
js
CopyEdit
console.log(a); // undefined
var a = 5;
console.log(b); // ReferenceError
let b = 10;
js
CopyEdit
Answer:
The Temporal Dead Zone is the time between the hoisting of a let or const variable and its actual initialization. Accessing
the variable in this zone causes a ReferenceError.
js
CopyEdit
console.log(x); // ReferenceError
let x = 10;
Answer:
Yes. JavaScript is a dynamically typed language, meaning you don't declare a variable's type. A variable can hold any
type, and the type can change at runtime.
js
CopyEdit
Answer:
Yes, but it’s not recommended. Declaring a variable without any keyword creates an implicit global variable, which can
lead to bugs.
js
CopyEdit
function example() {
example();
console.log(x); // 10
Answer:
Variable shadowing occurs when a variable declared inside a block (or function) has the same name as a variable in an
outer scope. The inner variable “shadows” the outer one in that scope.
js
CopyEdit
let x = 1;
function test() {
console.log(x); // 2
test();
console.log(x); // 1
js
CopyEdit
var a = 10;
let b = 10;
Answer:
An execution context is the environment in which JavaScript code runs. There are three types:
Variable Environment
Scope Chain
this binding
3. Explain what happens during the creation and execution phase of an execution context.
Answer:
Creation Phase:
o let and const are hoisted but kept in the Temporal Dead Zone.
Execution Phase:
Answer:
Hoisting moves variable and function declarations to the top of their scope during the creation phase. This means they
can be used before their actual line of code — but only in certain ways:
js
CopyEdit
console.log(a); // undefined
var a = 5;
console.log(b); // ReferenceError
let b = 10;
js
CopyEdit
function test() {
console.log(x); // ?
var x = 10;
console.log(x); // ?
test();
Answer:
Answer:
The call stack is a data structure that keeps track of function calls.
Example:
js
CopyEdit
function a() {
console.log("Inside a");
b();
}
function b() {
console.log("Inside b");
a();
1. Global → enters
2. a() pushed
4. b() pushed
6. b() popped
7. a() popped
js
CopyEdit
greet(); // ?
function greet() {
console.log("Hello");
Answer:
It works fine because function declarations are hoisted with their entire definition.
js
CopyEdit
sayHi(); // ?
Answer:
This will throw a TypeError because sayHi is hoisted as undefined (just like var), but it's not initialized until the
assignment line.
9. What will be the output?
js
CopyEdit
var a = 1;
function outer() {
var a = 2;
function inner() {
console.log(a);
inner();
outer();
Answer:
Output: 2
Because the inner() function has access to its outer lexical environment and picks a from outer()'s scope.
Answer:
No. JavaScript handles async code using the event loop, which defers execution using the task queue (for setTimeout,
etc.) or the microtask queue (for promises).
Example:
js
CopyEdit
console.log("A");
console.log("C");
Output:
css
CopyEdit