Skip to content
Fix Code Error

Early exit from function?

March 13, 2021 by Code Error
Posted By: Anonymous

I have a function:

function myfunction() {
  if (a == 'stop')  // How can I stop the function here?
}

Is there something like exit() in JavaScript?

Solution

You can just use return.

function myfunction() {
     if(a == 'stop') 
         return;
}

This will send a return value of undefined to whatever called the function.

var x = myfunction();

console.log( x );  // console shows undefined

Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.

return false;
return true;
return "some string";
return 12345;
Answered By: Anonymous

Related Articles

  • Javascript and css animation
  • How do the PHP equality (== double equals) and…
  • What does this symbol mean in JavaScript?
  • await is only valid in async function
  • how to check a pyspark dataframe value against…
  • problem with client server unix domain stream…
  • How to dispatch a Redux action with a timeout?
  • powershell regex bug
  • Logging best practices
  • Single test fails in PhantomJS but works in Chrome…
  • What is the scope of variables in JavaScript?
  • javascript .replace and .trim not working in vuejs
  • R - Using loops to search one variable with another…
  • For-each over an array in JavaScript
  • Rounding Bigdecimal values with 2 Decimal Places
  • AngularJS ui-router login authentication
  • Passport.js in Next.js app not saving user across…
  • Smart way to truncate long strings
  • LNK2019 símbolo externo public: bool __thiscall ……
  • Axios interceptor in vue 2 JS using vuex
  • How do I return the response from an asynchronous call?
  • Start redis-server with config file
  • Backbone Collection.fetch gives me Uncaught…
  • How do JavaScript closures work?
  • VueJs single file component not reading data/props/methods
  • Using Event Aggregator to load a view with different…
  • Property or method "key" is not defined on the…
  • How assignment works in JavaScript?
  • Convert array to nested JSON object - Angular Material tree
  • vuejs using arrow functions in @click events
  • How to resolve…
  • Dynamically update values of a chartjs chart
  • How to create websockets server in PHP
  • Callback functions in C++
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Why the REPL is not showing the full trace of the…
  • Sort table rows In Bootstrap
  • Returning a C string from a function
  • How do you round to 1 decimal place in Javascript?
  • how to use canvas in JavaScript flappy bird code
  • Perfomance issues with large number of elements in…
  • Angular/Typescript @Output with Union return types
  • Cannot observe properties of nested elements in…
  • Unhandled Promise Rejection when trying to call…
  • v-show doesn't work as expected
  • Backbone click event binding not being bound to DOM elements
  • How can I bind a boolean attribute in Polymer?
  • How to create a game over screen for a basic HTML/JS game?
  • Difference between return and exit in Bash functions
  • Addition of two queries - SQL/HANA
  • How to return values in javascript
  • How do you clear the SQL Server transaction log?
  • Managing longs and short orders like LucF PineCoders…
  • How to fix `no visible global function definition`…
  • TypeScript metadata reflection references other…
  • How to check for a logged in admin in polymerfire?
  • Am I paranoid? "Brutally" big Polymer website after…
  • WordPress/WooCommerce: Save custom payment post meta
  • Pass multiple column names in function to…
  • Active tab issue on page load HTML
  • What is a NullReferenceException, and how do I fix it?
  • How to filter a RecyclerView with a SearchView
  • Combining a class selector and an attribute selector…
  • SQL query return data from multiple tables
  • Linking of Page on same page with nextjs
  • How to properly do JSON API GET requests and assign…
  • Ember .transitionToRoute and currentModel issue
  • Playing HTML5 video on fullscreen in android webview
  • Javascript: How to pass a function with string…
  • How to terminate a Python script
  • How to write HTTP request for using third party API?
  • Gradient text color
  • dom-if does not update according to the condition
  • Regex : parentheses containing either only letters…
  • Repeat position within a loop in xslt
  • how to transform a JSON coming from an API into…
  • How do I split an int into its digits?
  • Correctly configure webpack-dev-middleware with…
  • Node.js/NodeMailer/Express/Outlook smtp host -…
  • Simplest way to create Unix-like continuous pipeline…
  • Why is my variable unaltered after I modify it…
  • How does the "this" keyword work?
  • How to "properly" create a custom object in JavaScript?
  • Why does my instance of Axios not return the…
  • Javascript and SVG - move element with offset
  • How to detect if multiple keys are pressed at once…
  • VueJs Router doesn't render componenet
  • Pure JavaScript equivalent of jQuery's $.ready() -…
  • Cannot call API by JavaScript from localhost
  • Programmatically Lighten or Darken a hex color (or…
  • Testing VueJS method inside of a promise which is…
  • What is an optional value in Swift?
  • Import functions from another js file. Javascript
  • Ember 2, filter relationship models (hasMany,…
  • i am making a suggestion bot in discord.py. it runs…
  • XMLHttpRequest cannot load ✘✘✘ No…
  • How to check file MIME type with javascript before upload?
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • How to use html template with vue.js
  • How can I access and process nested objects, arrays or JSON?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Is there any way to kill a Thread?

Next Post:

Trim string in JavaScript?

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error