diff --git a/src/data/question-groups/javascript/content/explicit-binding.md b/src/data/question-groups/javascript/content/explicit-binding.md new file mode 100644 index 000000000..c89dfcbdb --- /dev/null +++ b/src/data/question-groups/javascript/content/explicit-binding.md @@ -0,0 +1,19 @@ +Explicit binding is a way to explicitly state what the `this` keyword is going to be bound to using `call`, `apply` or `bind` methods of a function. + +```js +const roadmap = { + name: 'JavaScript', +}; + +function printName() { + console.log(this.name); +} + +printName.call(roadmap); // JavaScript +printName.apply(roadmap); // JavaScript + +const printRoadmapName = printName.bind(roadmap); +printRoadmapName(); // JavaScript +``` + +In the above example, the `this` keyword inside the `printName()` function is explicitly bound to the `roadmap` object using `call`, `apply` or `bind` methods. diff --git a/src/data/question-groups/javascript/content/set-interval.md b/src/data/question-groups/javascript/content/set-interval.md new file mode 100644 index 000000000..809be796f --- /dev/null +++ b/src/data/question-groups/javascript/content/set-interval.md @@ -0,0 +1,17 @@ +You can run some codes on interval using `setInterval` function in JavaScript. It accepts a function and a time interval in milliseconds. It returns a unique id which you can use to clear the interval using `clearInterval` function. + +```js +const intervalId = setInterval(() => { + console.log('Hello World'); +}, 1000); + +// Output: +// Hello World +// Hello World +``` + +In the above code, the `setInterval` function runs the callback function every 1000 milliseconds (1 second) and prints `Hello World` to the console. It returns a unique id which you can use to clear the interval using `clearInterval` function. + +```js +clearInterval(intervalId); +``` diff --git a/src/data/question-groups/javascript/content/set-timeout.md b/src/data/question-groups/javascript/content/set-timeout.md new file mode 100644 index 000000000..4cabf98e3 --- /dev/null +++ b/src/data/question-groups/javascript/content/set-timeout.md @@ -0,0 +1,16 @@ +To run a piece of code after a certain time, you can use `setTimeout` function in JavaScript. It accepts a function and a time interval in milliseconds. It returns a unique id which you can use to clear the timeout using `clearTimeout` function. + +```js +const timeoutId = setTimeout(() => { + console.log('Hello World'); +}, 1000); + +// Output: +// Hello World +``` + +In the above code, the `setTimeout` function runs the callback function after 1000 milliseconds (1 second) and prints `Hello World` to the console. It returns a unique id which you can use to clear the timeout using `clearTimeout` function. + +```js +clearTimeout(timeoutId); +``` diff --git a/src/data/question-groups/javascript/javascript.md b/src/data/question-groups/javascript/javascript.md index 3aeee808f..00236f390 100644 --- a/src/data/question-groups/javascript/javascript.md +++ b/src/data/question-groups/javascript/javascript.md @@ -211,4 +211,24 @@ questions: topics: - 'Core' - 'Advanced' + - question: What is Type Casting? + answer: Type conversion (or typecasting) means transfer of data from one data type to another. Implicit conversion happens when the compiler (for compiled languages) or runtime (for script languages like `JavaScript`) automatically converts data types. + topics: + - 'Core' + - 'Intermediate' + - question: What are Explicit binding in JavaScript? + answer: explicit-binding.md + topics: + - 'Core' + - 'Advanced' + - question: How to run a piece of code after a specific time interval? + answer: set-interval.md + topics: + - 'Core' + - 'Beginner' + - question: How to run a piece of code only once after a specific time? + answer: set-timeout.md + topics: + - 'Core' + - 'Beginner' ---