diff --git a/src/data/question-groups/javascript/content/break-and-continue.md b/src/data/question-groups/javascript/content/break-and-continue.md new file mode 100644 index 000000000..f07860b4f --- /dev/null +++ b/src/data/question-groups/javascript/content/break-and-continue.md @@ -0,0 +1,28 @@ +You can use `break` and `continue` in loops to alter the flow of the loop. `break` will stop the loop from continuing, and `continue` will skip the current iteration and continue the loop. + +```js +for (let i = 0; i < 5; i++) { + if (i === 1) { + continue; // skips the rest of the code in the loop + } + console.log(`i: ${i}`); +} + +// Output: +// i: 0 +// i: 2 +// i: 3 +// i: 4 +``` + +```js +for (let i = 0; i < 5; i++) { + if (i === 1) { + break; // stops the loop + } + console.log(`i: ${i}`); +} + +// Output: +// i: 0 +``` diff --git a/src/data/question-groups/javascript/content/debug-javascript.md b/src/data/question-groups/javascript/content/debug-javascript.md new file mode 100644 index 000000000..319764e68 --- /dev/null +++ b/src/data/question-groups/javascript/content/debug-javascript.md @@ -0,0 +1,38 @@ +Debugging JavaScript code can be achieved through various methods and tools. Here's a basic guide: + +## Console Logging: + +You can use `console.log()`, `console.warn()`, `console.error()`, etc., to print values, variables, or messages to the browser's developer console. + +```js +console.log('Value of x:', x); +``` + +## Browser Developer Tools: + +Most modern browsers come equipped with developer tools. You can access these tools by pressing `F12` or right-clicking on the web page and selecting `Inspect` or `Inspect Element`. + +- **Sources Tab**: Allows you to see the loaded scripts, set breakpoints, and step through the code. +- **Console Tab**: Displays console outputs and allows for interactive JavaScript execution. +- **Network Tab**: Helps in checking network requests and responses. + +## Setting Breakpoints: + +In the `Sources` tab of the browser's developer tools, you can click on a line number to set a breakpoint. The code execution will pause at this line, allowing you to inspect variables, the call stack, and continue step-by-step. + +## Debugger Statement: + +Inserting the `debugger;` statement in your code will act as a breakpoint when the browser developer tools are open. Execution will pause at the `debugger;` line. + +```js +function myFunction() { + debugger; // Execution will pause here when dev tools are open + // ... rest of the code +} +``` + +## Call Stack and Scope: + +In the developer tools, when paused on a breakpoint or `debugger;` statement, you can inspect the `call stack` to see the sequence of function calls. The `Scope` panel will show you the values of local and global variables. + +Remember, debugging is an iterative process. It often involves setting breakpoints, checking variables, adjusting code, and re-running to ensure correctness. diff --git a/src/data/question-groups/javascript/content/parse-json.md b/src/data/question-groups/javascript/content/parse-json.md new file mode 100644 index 000000000..be661bfe3 --- /dev/null +++ b/src/data/question-groups/javascript/content/parse-json.md @@ -0,0 +1,9 @@ +In order to parse JSON, you can use the `JSON.parse()` method. It parses a JSON string and returns the JavaScript equivalent. + +```js +const json = '{"name":"JavaScript","year":1995}'; +const roadmap = JSON.parse(json); + +console.log(roadmap.name); // JavaScript +console.log(roadmap.year); // 1995 +``` diff --git a/src/data/question-groups/javascript/content/variable-number-of-arguments.md b/src/data/question-groups/javascript/content/variable-number-of-arguments.md new file mode 100644 index 000000000..a7946db3e --- /dev/null +++ b/src/data/question-groups/javascript/content/variable-number-of-arguments.md @@ -0,0 +1,27 @@ +In JavaScript, you can accept a variable number of arguments in a function using the `arguments` object or the rest parameter (`...`). + +## Using the `arguments` object: + +The `arguments` is an array-like object that holds all of the passed arguments. They are only available inside the function body. + +```js +function displayArgs() { + for (let i = 0; i < arguments.length; i++) { + console.log(arguments[i]); + } +} +displayArgs(1, 2, 3, 4); // Outputs: 1, 2, 3, 4 +``` + +## Using the rest parameter: + +The rest parameter allows you to represent an indefinite number of arguments as an array. + +```js +function displayArgs(...args) { + args.forEach((arg) => console.log(arg)); +} +displayArgs(1, 2, 3, 4); // Outputs: 1, 2, 3, 4 +``` + +The rest parameter (`...args` in the example) is generally more modern and flexible, and it provides an actual array, unlike the array-like `arguments` object. diff --git a/src/data/question-groups/javascript/javascript.md b/src/data/question-groups/javascript/javascript.md index 4de946ea5..a36b5f0f1 100644 --- a/src/data/question-groups/javascript/javascript.md +++ b/src/data/question-groups/javascript/javascript.md @@ -246,4 +246,29 @@ questions: topics: - 'Core' - 'Beginner' + - question: How to accept variable number of arguments in a JavaScript function? + answer: variable-number-of-arguments.md + topics: + - 'Core' + - 'Intermediate' + - question: How to define multiline strings in JavaScript? + answer: In order to define multiline strings in JavaScript, you need to use template literals. Template literals are enclosed by the backtick (```` ` ` ````) character instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (``` `${expression}` ```). + topics: + - 'Core' + - 'Beginner' + - question: Uses of `break` and `continue` statements in JavaScript? + answer: break-and-continue.md + topics: + - 'Core' + - 'Beginner' + - question: How to parse JSON in JavaScript? + answer: parse-json.md + topics: + - 'Core' + - 'Beginner' + - question: How to debug JavaScript code? + answer: debug-javascript.md + topics: + - 'Core' + - 'Beginner' ---