Unable to understand why the following JavaScript function is not working

Related searches

I am new to Java Script and I am unable to understand why the following function is not working, what am I doing wrong here? Its prompting me to enter the var gender value but once I enter it is not executing any further. Given below the JavaScript code I am trying to run.

var gender = prompt('What is your gender..?');

function name(fname = 'Sayantan', mname, lname = 'Choudhury') {
  if (gender == male) {
    document.write('Hello Mr. ' + fname + ' ' + mname + ' ' + lname + '<br/>');
  } else {
    document.write('Hello Mrs. ' + fname + ' ' + mname + ' ' + lname + '<br/>');
  }
}

name('Mridula', 'Dutta', 'Chowdhury');
name('Mridula');

male is not variable here so you want to compare it with string like 'male' You also should check by converting gender to lower case so it can support case insensitive.

also there is one more problem if you don't pass mname you will see undefined in-between first and last name.

var gender = prompt('Gender');
function name(fname = 'Sayantan', mname = '', lname = 'Choudhury') {
  let title = 'Mrs'
  if (gender.toLowerCase() === 'male') {
    title = 'Mr';
  }
  document.write('Hello'+ title + '. ' + fname + ' ' + mname + (mname ? ' ' : '') + lname + '<br/>')
}

name('Mridula', 'Dutta', 'Chowdhury');
name('Mridula');

Handling common JavaScript problems, Now we'll look at common cross-browser JavaScript problems and how to fix them. Objective: To be able to diagnose common JavaScript cross-browser problems, and use For example, the following command installs JSHint: For a quick example, see arrow-function.html (see the source code also). Javascript not working Hi, I've got a problem, i'm using windows 10, and i tried all Browser like Edge, Chrome, Opera, Firefox the problem is Javascript buttons and hyperlinks are non-functional.


If you use male as male instead of 'male' javascript thinks that male is a variable declared somewhere in your code and throws an error because it doesn't exist(at least in your case), but what you enter inside prompt is stored in gender variable and it contains the string 'male' which you entered so the condition should be:

if(gender === 'male') 

or

if(gender === 'female') 

=== compares the type along with value, if you only want to compare the value then you can use ==.

What went wrong? Troubleshooting JavaScript, It won't always be that simple to work out what's wrong in your code, but at least addeventListener is not a function"; A "Learn More" link that links If you follow this link, you'll see the exact line where the error is highlighted. At that point, the game fails again, and the same error is spat out that we got at� Hi Experts, I am new in VS 2005. I have been working with asp classic. Now I am working with VS 2005 I tried to put a javascript function for a button click. Its not working.


Attention in your code gender == male : whether male is a variable or male is a string 'male' ?

I suggest that you want to write the string 'male' because I don't see the declaration of the variable male. So in this case, you must write the code : gender === "male"

Moreover, I don't neither see the declaration of the variable gender in your code. I recommend you be attention for declaration of identifier names (variables and functions) in the code. Example : so you could respect the camelCase. It's a good pratice !

An example :

function fncCheckName (name, mName, lName) { }

You could view also my answer in the another post : https://stackoverflow.com/a/60037641/4386148

Functions, Please note: the function changes from , but the change is not seen With prefixes in place, a glance at a function name gives an understanding what kind of work it The following function returns true if the parameter age is greater than 18 . I should not be able to access name variable inside function� JavaScript cannot access databases unless you use Ajax and have a server-side script to perform the database accesses for you. It Cannot Read From or Write to Files in the Client Even though JavaScript is running on the client computer (the one where the web page is being viewed) it is not allowed to access anything outside of the web page itself.


Quick Tip to Avoid JavaScript Function Not defined error!, Do your SharePoint or Dynamics CRM JavaScript functions sometimes throw for me to see the function definition as “var <FunctionName> = function()”. This is � I disabled all add-ons and went to the default theme for 8.0. This did not solve the problem. When accessing the online banking site it still gives me the pop-up "javascript needs to be enabled to access site." I check my preferences and javascript is enabled. I've tried this in Safari and Chrome and have no problems accessing the site.


Top 10 JavaScript errors from 1000+ projects (and how to avoid them), There are two important things realize here: When this is the case, the problem 99.9% of the time is IE's inability to bind methods within Rollbar namespace you can invoke the isAwesome method with the following syntax:. Undefined is Not Null. JavaScript objects, variables, properties, and methods can be undefined. In addition, empty JavaScript objects can have the value null. This can make it a little bit difficult to test if an object is empty. You can test if an object exists by testing if the type is undefined:


To understand what’s going on, we need to better understand how things are working in JavaScript under the hood. The typical way that closures are implemented is that every function object has a link to a dictionary-style object representing its lexical scope.