1

Can someone help me understand what the next line means? What is -

elem,.style.height = ( pos /100) * h + "px";

There's a comma right after elem.

function slideDown( elem ){
 // 
 elem.style.height = '0px';
 show(elem);

 var h = fullHeight(elem);

 for (var i = 0 ; i <= 100; i+= 5){
  (function(){
   var pos = i; 

   setTimeout(function(){
    elem,.style.height = ( pos /100) * h + "px";
   }, (pos + 1) * 10 );
  })();
 }
}
4
  • 4
    That's garbage (as in unintentionally typed character)... remove that comma. Commented Nov 8, 2010 at 21:27
  • 1
    That's clearly a mistake. Your browser should show you an error when you load the page. Commented Nov 8, 2010 at 21:28
  • When I run the program without the comma, i get a elem is not defined. It runs with the comma. Commented Nov 8, 2010 at 21:31
  • What are you passing in into the slideDown function (when you are calling it)? Commented Nov 8, 2010 at 22:01

3 Answers 3

2

But let's explain the comma punctuator in JavaScript, shall we? :)

The comma can be either a separator or an operator. As a separator, it appears in these scenarios:

function foo(x, y, z) { /* function body */ }
foo(1, 2, 3);
var obj = { name: "John", surname: "Smith", age: 35 };
var arr = [1, 2, 3];
var x = 1, y = 2, z = 3;

This is not a complete list, but I think I covered the most popular scenarios (separating formal parameters in function declarations/expressions, arguments in function calls, object literal items, array literal items, and variable declarations).

As a operator, the comma can be used to list expressions:

x = 1, foo(), location.href, bar(), x = 2;

The comma operator should be avoided (" except for very disciplined use in the control part of for statements" - Crockford, http://javascript.crockford.com/code.html)

Sign up to request clarification or add additional context in comments.

Comments

1

It is a typo, but it's also almost valid syntax.

The comma may be used to separate statements, returning the last. So this is valid:

a = 3;
a, b = 4;

However, the .style in the second clause of the comma is invalid syntax (vars cannot begin with period, and style is not defined, so it can't have the property of height).

Remove the comma.

3 Comments

Why doesn't the debugger catch the .style.height if i remove the comma? Also when i remove the comma, it says elem is undefined. Why is that?
@steve The elem identifier is passed in into the function as the argument. If it is undefined, than that means that you called the function without passing in an argument.
"The comma may be used to separate statements" - this is not correct. In your code, the comma is used as an operator, not as a separator. And the operands are not statements, but expressions.
-2

A comma will have the role of operator outside a string. Your example is clearly a mistake.

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

Source: MDC

2 Comments

You say that a comma cannot be used outside of a string, then you give the explanation of where it could be used outside of a string.
@Zack Bloom -.- What I was trying to say is that you cannot use it outside a string without the function as operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.