JS Flashcards
Interview
What do you understand about JavaScript?
JavaScript is a popular web scripting language and is used for client-side and server-side development. It enables interactive and dynamic content on websites.
It’s capable of:
- Adding Interactivity: JavaScript makes web pages more engaging by allowing developers to create responsive elements like buttons, forms, animations, and more.
- Manipulating Web Page Content: It can modify HTML and CSS, changing the structure and appearance of a webpage dynamically.
- Handling Events: JavaScript can respond to user actions like clicks, keystrokes, and mouse movements to trigger specific actions or functions.
- Data Handling: It can store and manage data, perform calculations, and interact with databases.
- Creating Web Applications: With frameworks like React, Angular, or Vue.js, JavaScript is used to build complex and efficient web applications.
What’s the difference between JavaScript and Java?
JavaScript
- JavaScript is an object-oriented scripting language.
- JavaScript applications are meant to run inside a web browser.
- JavaScript does not need compilation before running the application code.
Java
- Java is an object-oriented programming language.
- Java applications are generally made for use in operating systems and virtual machines.
- Java source code needs a compiler before it can be ready to run in realtime.
What are the various data types that exist in JavaScript?
- Boolean - For true and false values
- Null - For empty or unknown values
- Undefined - For variables that are only declared and not defined or initialized
- Number - For integer and floating-point numbers
- String - For characters and alphanumeric values
- Object - For collections or complex values
- Symbols - For unique identifiers for objects
What is the difference between null and undefined?
- undefined: Usually means a variable has been declared but not assigned a value, or a function without a return statement returns undefined.
- null: Specifically set by a programmer to indicate an intentional absence of value or to clear a variable.
What are the features of JavaScript?
These are the features of JavaScript:
- Lightweight, interpreted programming language
- Cross-platform compatible
- Open-source
- Object-oriented
- Integration with other backend and frontend technologies
- Used especially for the development of network-based applications
What are the advantages of JavaScript over other web technologies?
These are the advantages of JavaScript:
- Enhanced Interaction
JavaScript adds interaction to otherwise static web pages and makes them react to users’ inputs. - Quick Feedback
There is no need for a web page to reload when running JavaScript. For example, form input validation. - Rich User Interface
JavaScript helps in making the UI of web applications look and feel much better. - Frameworks
JavaScript has countless frameworks and libraries that are extensively used for developing web applications and games of all kinds.
What are some of the built-in methods in JavaScript?
- String Methods:
length: Returns the length of a string.
charAt(index): Returns the character at the specified index.
toUpperCase(), toLowerCase(): Converts a string to uppercase or lowercase.
concat(): Joins two or more strings.
indexOf(substring), lastIndexOf(substring): Finds the index of a substring within a string.
slice(start, end): Extracts a portion of a string. - Array Methods:
push(), pop(): Add/remove elements from the end of an array.
shift(), unshift(): Add/remove elements from the beginning of an array.
concat(): Joins two or more arrays.
join(separator): Joins all elements of an array into a string.
slice(start, end): Extracts a portion of an array.
indexOf(element), lastIndexOf(element): Finds the index of an element within an array.
forEach(), map(), filter(), reduce(): Iterating and performing operations on array elements. - Object Methods:
Object.keys(obj): Returns an array of an object’s keys.
Object.values(obj): Returns an array of an object’s values.
Object.entries(obj): Returns an array of an object’s key-value pairs. - Number Methods:
toString(): Converts a number to a string.
toFixed(), toPrecision(): Formats a number to a specified length or precision.
parseInt(), parseFloat(): Parses a string and returns an integer or floating-point number. - Other Useful Methods:
setTimeout(), setInterval(): Functions to execute code after a specified delay or at specified intervals.
JSON.parse(), JSON.stringify(): Methods to convert JSON data to/from JavaScript objects.
What are the scopes of a variable in JavaScript?
The scope of a variable implies where the variable has been declared or defined in a JavaScript program. There are two scopes of a variable:
- Global Scope
Global variables, having global scope are available everywhere in a JavaScript code. - Local Scope
Local variables are accessible only within a function in which they are defined.
What is the ‘this’ keyword in JavaScript?
The ‘this’ keyword in JavaScript refers to the currently calling object. It is commonly used in constructors to assign values to object properties.
- Global Context:
In the global context (outside of any function), this refers to the global object (window in browsers, global in Node.js). - Function Context:
Inside a function, this refers to the global object in non-strict mode but becomes undefined in strict mode if not explicitly set or if the function is not called on any object. - Object Method:
When a function is a method of an object, this refers to the object that owns the method being called.
Constructor Context: - Inside a constructor function (used with new keyword to create objects), this refers to the specific instance of the object being created by that constructor.
Explicitly Bound Context: - Using methods like call(), apply(), or bind() allows explicitly defining what this refers to within a function, overriding its default context.
What are the conventions of naming a variable in JavaScript?
Following are the naming conventions for a variable in JavaScript:
- Variable names cannot be similar to that of reserved keywords. For example, var, let, const, etc.
- Variable names cannot begin with a numeric value. They must only begin with a letter or an underscore character.
- Variable names are case-sensitive.
What is Callback in JavaScript?
In JavaScript, a callback is a function passed as an argument to another function to be executed later. Callbacks are commonly used in asynchronous operations or when you want a function to be executed after another function has finished its execution.
What is the difference between Function declaration and Function expression?
- Function declaration
Declared as a separate statement within the main JavaScript code .
Can be called before the function is defined.
function abc() {
return 5;
} - Function expression
Created inside an expression or some other construct.
Created when the execution point reaches it; can be used only after that.
const greet = function() {
return “Hello!”;
};
var a = function abc() {
return 5;
}
What are the ways of adding JavaScript code in an HTML file?
There are primarily two ways of embedding JavaScript code:
- We can write JavaScript code within the script tag in the same HTML file; this is suitable when we need just a few lines of scripting within a web page.
- We can import a JavaScript source file into an HTML document; this adds all scripting capabilities to a web page without cluttering the code
What do you understand about cookies?
A cookie is generally a small data that is sent from a website and stored on the user’s machine by a web browser that was used to access the website. Cookies are used to remember information for later use and also to record the browsing activity on a website.
How would you create a cookie?
Structure :
document.cookie = “key1 = value1; key2 = value2; expires = date”;
Example :
// Function to set a cookie
function setCookie(cookieName, cookieValue, expirationDays) {
const d = new Date();
d.setTime(d.getTime() + (expirationDays * 24 * 60 * 60 * 1000)); // Set expiration time
const expires = "expires=" + d.toUTCString(); document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/"; }
// Usage: Set a cookie with the name “username” and value “John Doe” that expires in 7 days
setCookie(“username”, “John Doe”, 7);
Result of Example
document.cookie = “username=John Doe; expires=Thu, 17 Dec 2024 12:00:00 UTC; path=/”;