Chai Wala Chai Wala - 3 years ago 170
Javascript Question

What do the Semicolon and Identifier do in JavaScript?

I have just started to learn JavaScript from Lynda.com, and I am now working on the lesson about functions.

I am confused about the anonymous function. When I wrote the code below my IDE showed some warnings which I don't understand.

var theBiggest = function(a,b) {
"use strict";
var result;
a>b ? result = ["a", a] : result = ["b", b];
console.log(result);
})(5/7,18/25)

console.log(theBiggest);


The warnings are:


1). Missing semicolon on line 6 of code.

2). Expected an assignment or function call and instead saw an
expression on line 4.

Expected an identifier and instead saw ')' on line 6.


The tutorial has same code as me but mine isn't working.

Answer Source

A semicolon is the ";" character which ends a statement. Normally, js doesn't really care wether you put a ; at the end of every statement, but because you use strict mode, you are obligated to put it there.

After the closing }-bracket on line six, I see a closing )-bracket, but its opening bracket is nowhere to be found. What you are doing here is an immediate function (function definition and a call to that function at the same time). Usually when using immediate functions, the whole function is surrounded by () brackets to make it clear you are actually calling the function and not assigning it to a variable (theBiggest in this case)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download