Q:

What is the difference between a JS object literal and a JSON string?

I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:
{foo: 'bar', bar : 'baz'}

AFAIK, above is object literal, json as well as javascript object, isn't it?
Does object literal and json mean the same thing ?
How do you guys differentiate which is what?

A:

The variable jsonString contains a JSON string:
var jsonString = '{"foo": "bar", "bar" : "baz"}'

The variable javascriptObject contains a javascript object, initialized using an object literal:
var javascriptObject =  {foo: 'bar', bar : 'baz'}

You can convert a json string to a javascript object with JSON.parse, and back again with JSON.stringify.

A:

JSON is a just a data format, like XML. True JSON should have the keys surrounded by double quotes, like so:
{"foo":"bar"}

JavaScript Objects are part of the JavaScript language, and have associated things such as a prototype.
Object literals is creating a javascript object in place with brackets as opposed to using the new keyword, or Object.create().  
//object literal
var foo = {};

//equivalent
var foo = new Object();

A:

AFAIK, above is object literal, json as well as javascript object, isn't it?

It is an object literal. It creates an object.
It is not JSON, as it doesn't conform to the syntax (which is a subset of object literal notation). The keys are not quoted and the wrong kind of quote marks (' instead of ") are used around the values.

How do you guys differentiate which is what ?

Context.
JSON doesn't usually appear (embedded) in the middle of JavaScript programs. It is a data format and usually appears as whole files (or HTTP responses).
When something expects an object it could get one from an object literal or from a variable (or a return value from a function call, etc, etc).

