JavaScript - Basic Flashcards
What are the key features of JavaScript?
- Dynamic and weak typing
- Prototype-based object-oriented programming
- Closures
- First-class functions
- Event-driven programming
- Asynchronous programming
Dynamic and weak typing
- Variables can hold values of any data type,
- and their types can be changed during runtime.
Closures
allow inner functions to access variables from their outer function scopes.
First-class functions
Functions can be:
- assigned to variables,
- passed as arguments,
- and returned as values.
How do you include JavaScript code in an HTML file?
inline: You can write JavaScript directly within the HTML file using the script tag and placing the code between the opening and closing tags.
External file: You can also link an external JavaScript file using the src attribute of the script tag.
What are the primitive data types in JavaScript?
- Number
- String
- Boolean
- Undefined
- Null
- Symbol
Explain the concept of hoisting in JavaScript.
- variable and function declarations are moved to the top of their containing scope during the compilation phase.
- This means that you can call functions before they are declared in their scope.
- For let/const, only the declarations are hoisted, not the initializations or assignments.
- For var, it is initialized with the value of undefined until it is declared
What are the different ways to define variables in JavaScript?
- var
- let
- const
How do you check the data type of a variable in JavaScript
typeof
For example:
let num = 10; console.log(typeof num); // Outputs: "number"
.
message = "Hello"; console.log(typeof message); // Outputs: "string"
.
let isValid = true; console.log(typeof isValid); // Outputs: "boolean"
undefined
- It indicates that a variable has been declared but has not been assigned a value.
null
- It represents the intentional absence of any object value.
- It is typically assigned by developers to indicate that a variable or object property should have no value.
- Unlike undefined, null is an explicitly assigned value.
What are the arithmetic operators in JavaScript?
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Modulus (remainder): %
- Exponentiation: **
- Increment: ++
- Decrement: –
What is the purpose of the “this” keyword in JavaScript?
- The this keyword in JavaScript refers to the object on which a method is being invoked.
For example:
const person = { name: "John", sayHello: function() { console.log("Hello, " + this.name); } }; person.sayHello(); // Outputs: "Hello, John"
In the above code, this.name accesses the name property of the person object using the this keyword.
Explain the concept of closures in JavaScript.
An inner function always has access to the variables defined in the outer functions even if the outer function has finished executing.
For Example:
function outer() { let outerVariable = "I'm from the outer function"; function inner() { console.log(outerVariable); } return inner; } const closureFunction = outer(); closureFunction(); // Outputs: "I'm from the outer function"
In the above code, the inner function forms a closure with the outer function’s scope. Even after the outer function has returned, the closureFunction still has access to the outerVariable variable.
What is event delegation in JavaScript?
- attach a single event listener to a parent element and use event bubbling to handle events instead of attaching multiple event listeners to it’s descendant elements.
How does prototypal inheritance work in JavaScript?
- In JavaScript, prototypal inheritance is a way of creating objects based on other objects.
- Each object in JavaScript has an internal property called [[Prototype]] that points to its prototype object.
- When you access a property or method on an object, JavaScript first looks for it on the object itself. If it doesn’t find it, it continues to search for the property or method on the object’s prototype, and so on, forming a chain.
For example:
// Prototype object const animal = { sound: "Sound not defined", makeSound: function() { console.log(this.sound); } }; // Object inheriting from the prototype const dog = Object.create(animal); dog.sound = "Woof"; dog.makeSound(); // Outputs: "Woof" // Object inheriting from the same prototype const cat = Object.create(animal); cat.sound = "Meow"; cat.makeSound(); // Outputs: "Meow"
In the above code, both the dog and cat objects inherit the sound property and makeSound method from the animal prototype object using Object.create()
What is the purpose of the “use strict” directive in JavaScript?
- When strict mode is enabled, JavaScript enforces stricter rules and generates more errors in certain situations.
- i.e. this in functions aren’t bound to the global variable (i.e. the window object if in a browser).
How do you create an object in JavaScript?
- Object literals- just write the object out
- Constructor function - create a function and then use new to create the object
- Object.create()