Jump to content

JAVASCRIPT: return statement in normal vs arrow functions.

Go to solution Solved by Vicarian,
8 hours ago, shivajikobardan said:

Source code: https://flexiple.com/javascript/javascript-filter-array/

let freelancers = [{name: "Harry", skill: "JavaScript"},{name: "Mark", skill: "Python"},{name: "David", skill:"JavaScript"}];

let javascript_freelancers = freelancers.filter(function(freelancer) {
    return freelancer.skill == "JavaScript"; });

console.log(javascript_freelancers);


My code:

let freelancers = [{ name: "Harry", skill: "JavaScript" }, { name: "Mark", skill: "Python" }, { name: "David", skill: "JavaScript" }];

let newfreelancers = freelancers.filter((obj) => obj.skill == "Javascript");
console.log(newfreelancers);

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?
 

String comparisons are case sensitive.  You'd want to include a toUpperCase() or toLowerCase() call on the skill property and compare against an appropriate string literal to make sure you're being case-insensitive.  Put a different way, your data has the string "JavaScript" but you're filtering for "Javascript"  The strings don't match.  The reason it works with the single = sign is because you're assigning the data, not reading it.  With the assignment, you'll notice that Mark suddenly knows how to do Javascript, when he should have Python.

Source code: https://flexiple.com/javascript/javascript-filter-array/

let freelancers = [{name: "Harry", skill: "JavaScript"},{name: "Mark", skill: "Python"},{name: "David", skill:"JavaScript"}];

let javascript_freelancers = freelancers.filter(function(freelancer) {
    return freelancer.skill == "JavaScript"; });

console.log(javascript_freelancers);


My code:

let freelancers = [{ name: "Harry", skill: "JavaScript" }, { name: "Mark", skill: "Python" }, { name: "David", skill: "JavaScript" }];

let newfreelancers = freelancers.filter((obj) => obj.skill == "Javascript");
console.log(newfreelancers);

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?
 

Link to post
Share on other sites

8 hours ago, shivajikobardan said:

Source code: https://flexiple.com/javascript/javascript-filter-array/

let freelancers = [{name: "Harry", skill: "JavaScript"},{name: "Mark", skill: "Python"},{name: "David", skill:"JavaScript"}];

let javascript_freelancers = freelancers.filter(function(freelancer) {
    return freelancer.skill == "JavaScript"; });

console.log(javascript_freelancers);


My code:

let freelancers = [{ name: "Harry", skill: "JavaScript" }, { name: "Mark", skill: "Python" }, { name: "David", skill: "JavaScript" }];

let newfreelancers = freelancers.filter((obj) => obj.skill == "Javascript");
console.log(newfreelancers);

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?

My code works when I remove 1 "=" from obj.skill=="Javascript". Why it's like that?
 

String comparisons are case sensitive.  You'd want to include a toUpperCase() or toLowerCase() call on the skill property and compare against an appropriate string literal to make sure you're being case-insensitive.  Put a different way, your data has the string "JavaScript" but you're filtering for "Javascript"  The strings don't match.  The reason it works with the single = sign is because you're assigning the data, not reading it.  With the assignment, you'll notice that Mark suddenly knows how to do Javascript, when he should have Python.

Link to post
Share on other sites

18 minutes ago, Vicarian said:

String comparisons are case sensitive.  You'd want to include a toUpperCase() or toLowerCase() call on the skill property and compare against an appropriate string literal to make sure you're being case-insensitive.  Put a different way, your data has the string "JavaScript" but you're filtering for "Javascript"  The strings don't match.  The reason it works with the single = sign is because you're assigning the data, not reading it.  With the assignment, you'll notice that Mark suddenly knows how to do Javascript, when he should have Python.

thank you a lot.

Link to post
Share on other sites

20 hours ago, Vicarian said:

The reason it works with the single = sign is because you're assigning the data, not reading it.  With the assignment, you'll notice that Mark suddenly knows how to do Javascript, when he should have Python.

Still bedazzle me that assignation return a true/false predicate for the filter to consume.

Thank you for keeping me hating javascript unintuitive language

Link to post
Share on other sites

17 hours ago, Franck said:

Still bedazzle me that assignation return a true/false predicate for the filter to consume.

Thank you for keeping me hating javascript unintuitive language

The predicates expect a Boolean, but as soon as the assignment happens, it's left with a var whose value is neither undefined nor null.  JS has overloads as many other languages do to handle null checks with a simple conditional.  It also works for undefined.  In any browser console, try:
 

var n = null;
var u = undefined;

if (n) {
  console.log('hi, I won\'t print because I\'m null!');
}

if (u) {
  console.log('hi, I won\'t print because I\'m undefined!');
}

Any non-Boolean var that is neither undefined nor null will return true if used in this manner.  It's a handy shortcut for testing for both of these cases.

Edited by Vicarian
Clarity
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×