Warning: session_start(): open(/tmp/sess_9cb5274426b27bc4cc53bddf50eacda9, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Solve The SyntaxError: Illegal Break Statement In JavaScript? - LearnShareIT

How To Solve The SyntaxError: Illegal Break Statement In JavaScript?

SyntaxError: Illegal break statement in JavaScript

This guide can help you learn how to solve the SyntaxError: Illegal break statement in JavaScript. Follow this article to learn more about it with the explanation and examples below.

How does the SyntaxError: Illegal break statement in JavaScript happen?

The SyntaxError: Illegal break statement in JavaScript occurs when you misuse the break statement. For example, you try to use the break statement in the function, or you use it outside the loop. If you do like that, this error will occur.

Look at the example to learn more about this error.

function example(){
  let result = ''
  const arr = ['Learn', 'Share', 'IT', 'crvt4722']
  arr.forEach(str => {
  if (str == 'crvt4722') {
    break // The Error: SyntaxError: Illegal break statement will occur here
  }
  console.log(str)
  })
}

example()

Output

SyntaxError: Illegal break statement

In this example, this error occurs because you use the break statement in the function in the for loop.

How to solve this error?

These are some solutions that can help you solve the SyntaxError: Illegal break statement. If you want to terminate the function when it meets a specific condition, you can use the return statement instead. Or if you are using the forEach() function to iterate the array, you can use the ‘for … of’ loop instead. Another solution is using the try-catch method.

Use the return statement instead

If you want to terminate the function when it meets a specific condition, you can use the return statement instead.

Look at the example below to learn more about this solution.

function example(){
  const arr = ['Learn', 'Share', 'IT', 'crvt4722']
  arr.forEach(str => {
  if (str == 'crvt4722') {
    // If the String in the Array is equal to 'crvt4722'. The function will terminate.
    return 
  }
  console.log(str)
  })
}

example()

Output

Learn
Share
IT

Use the ‘for … of’ loop

If you use the forEach() method and break inside it in the function, the error will occur. Try to use the ‘for … of’ loop instead.

Look at the example below to learn more about this solution.

function example(){
  const arr = ['Learn', 'Share', 'IT', 'crvt4722']
  for (let str of arr){
  if (str == 'crvt4722') {
    // If the String in the Array is equal to 'crvt4722'. The loop will terminate
    break
  }
  console.log(str)
  }
}

example()

Output

Learn
Share
IT

Use the try-catch method

You can use the try-catch method instead to break the loop.

Look at the example below to learn more about this solution.

function example(){
  const arr = ['Learn', 'Share', 'IT', 'crvt4722']
  try {
    arr.forEach(str => {
      if (str == 'crvt4722') {
        // If the String in the Array is equal to 'crvt4722'. The loop will terminate.
        throw SyntaxError
      }
      console.log(str)
      })
    
  } catch (error){
    if (error != SyntaxError) throw error
  }
}

example()

Output

Learn
Share
IT

Summary

To solve the SyntaxError: Illegal break statement in JavaScript, you can use the return statement, the ‘for … of’ loop, or the try-catch method instead of the break statement. Choose the solution that is best for you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *