parent
3fab75d44c
commit
1604cb9d8c
5 changed files with 127 additions and 0 deletions
@ -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 |
||||||
|
``` |
@ -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. |
@ -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 |
||||||
|
``` |
@ -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. |
Loading…
Reference in new issue