JavaScript Basics Flashcards
JavaScript Basics (30 cards)
What are the primitive data types in JavaScript?
The primitive data types in JavaScript are:
* String
* Number
* Boolean
* Null
* Undefined
* Symbol
* BigInt
None
Explain the difference between null and undefined.
Undefined means a variable has been declared but not assigned a value. Null is an explicit assignment to represent ‘no value’ or ‘empty’.
Undefined is a type itself, while null is an object (though this is considered a bug in JavaScript)
What is the difference between let, const, and var?
var is function-scoped and hoisted.
let is block-scoped, can be reassigned, but not redeclared in the same scope.
const is block-scoped, cannot be reassigned or redeclared, but object properties can still be modified.
None
What is variable hoisting?
Hoisting is JavaScript’s behavior of moving declarations to the top of their containing scope during compilation.
- Function Declarations: Function declarations are fully hoisted, meaning you can call a function before it is defined in the code.
- var are hoisted and initialized with undefined
- let and const are hoisted but not initialized (temporal dead zone)
What are the different ways to create a function in JavaScript?
Function declaration: function name() {};
Function expression: const name = function() {};
Arrow function: const name = () => {};
Method in object: const obj = { name() {} }.
None
What is a closure in JavaScript?
A closure is a function that has access to its own scope, the scope of the outer function, and global scope, even after the outer function has returned.
None
How do arrow functions differ from regular functions?
- ‘This’ binding is from surrounding context
- don’t have their own arguments object
- can’t be used as constructors
- have implicit return for one-liners
None
What is a callback function?
A callback function is a function passed as an argument to another function, which is then invoked inside the outer function to complete some action.
None
Explain the concept of ‘this’ in JavaScript.
‘this’ refers to the object that is executing the current function. Its value depends on how the function is called.
In methods, it refers to the owner object; in regular functions, it refers to the global object (or undefined in strict mode); in events, it refers to the element that received the event.
What are the ways to explicitly set the value of ‘this’?
You can explicitly set ‘this’ using:
* call()
* apply()
* bind()
None
What is the difference between synchronous and asynchronous code?
Synchronous code executes sequentially line by line, blocking further execution until the current operation completes. Asynchronous code allows operations to run in the background without blocking the main thread.
It uses callbacks, promises, or async/await to handle results.
Explain promises in JavaScript.
Promises are objects representing the eventual completion or failure of an asynchronous operation. They have three states:
* pending
* fulfilled (resolved)
* rejected
They use .then() for success handlers and .catch() for error handlers.
What is the difference between promises and async/await?
Async/await is syntactic sugar over promises, making asynchronous code look and behave more like synchronous code.
async functions always return promises, and await can only be used inside async functions to pause execution until a promise resolves.
Explain the event loop in JavaScript.
The event loop continuously checks if the call stack is empty. If empty, it takes the first task from the queue and pushes it onto the stack for execution.
This enables JavaScript’s non-blocking, asynchronous behavior despite being single-threaded.
What is the difference between map, filter, and reduce?
map() creates a new array by transforming each element.
filter() creates a new array with elements that pass a test function.
reduce() accumulates array elements into a single value based on a function with an accumulator.
None
How do you clone an object in JavaScript?
- Object.assign({}, obj)
- spread operator ({…obj}).
- JSON.parse(JSON.stringify(obj)),
- using libraries like Lodash’s cloneDeep, or the structured clone API.
Deep cloning has limitations.
What is destructuring in JavaScript?
Destructuring is a syntax that allows you to extract multiple properties from arrays or objects into distinct variables:
* const {name, age} = person; for objects
* const [first, second] = array; for arrays.
None
How do you check if a property exists in an object?
- Using the “in” operator (console.log(‘name’ in obj)
- Using the hasOwnProperty() method. (console.log(obj.hasOwnProperty(‘name’));)
- Check for “undefined”
- Use obj.keys() to return array of property names.
None
What are template literals?
Template literals are string literals allowing embedded expressions and multi-line strings, denoted with backticks (``). They allow string interpolation with ${expression} syntax.
None
What String methods would you use to transform ‘hello world’ to ‘Hello World’?
You can split the string, capitalize the first letter of each word, and join it back:
* ‘hello world’.split(‘ ‘).map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(‘ ‘).
None
How would you check if a string contains a specific substring?
Using includes(): string.includes(substring)
None
What are the benefits of using ES6 modules?
ES6 modules provide:
* Better organization
* Explicit imports/exports
* Better dependency management
* Tree Shaking. Eliminate unused code, smaller bundle
* Reusability
* Encapsulation
* Modularity
None
What is the spread operator and how is it used?
- It allows an iterable (like an array or an object) to be expanded into individual elements or properties.
- It is commonly used for array manipulation, function arguments, and object cloning or merging.
- Array concatenation
- Copying arrays/objects
- Spreading arguments in function calls
- Gathering rest parameters
None
What is the optional chaining operator (?.) and when would you use it?
Allows you to safely access deeply nested properties of an object without having to explicitly check if each reference in the chain is null or undefined.
- const city = user.profile?.address?.city; // ‘Wonderland’
- const zipCode = user.profile?.address?.zipCode; // undefined
It returns undefined if any reference is null or undefined instead of throwing an error.