I am doing javascript validation for empty checking of a text field in my form.. I want to know which of the following is the best method and why?
Method 1
if($("#myField").val()==''){
alert("Cannot be empty");
}
Method 2
var check=$("#myField").val();
if(check==''){
alert("Cannot be empty");
}
Which of them is best?? Method 1 or Method 2..And why??
Use method 1 only, if you dont need the value of that input field again later in your code.
Use method 2 if you are reusing that value later in your code. But I’d advise to use a more meaningful variable name like fieldValue instead of check.