What is CoffeeScript all about?

Share this article

Curious about CoffeeScript, are you? This should give you a quick idea of what it’s all about! Our lead developer Paul Annesley and I took a break from the daily grind here in the SitePoint offices to put this video together for you. CoffeeScript is rumored to be made by people who dislike JavaScript syntax for people who equally dislike JavaScript syntax. It might even go so far as to help people who don’t understand how JavaScript works at its roots to write more optimized code, since the compilation into JavaScript is implemented pretty well from a performance point of view. Would you agree?

Check out the video and let us know your thoughts!

Test your CoffeeScript knowledge with our new Quiz here Buy our latest Jump Start book on CoffeeScript JGB: I’m here with Paul Annesley, who is the lead developer at the SitePoint Group. Paul is going to introduce us to CoffeeScript. So, what exactly is CoffeeScript? Paul: CoffeeScript is a programming language which compiles to JavaScript. That means that you write your code in CoffeeScript, run it through the compiler, and the output is JavaScript that can be served up to your browser. JGB: If I’m a JavaScript programmer, why would I want to use CoffeeScript instead? Paul: CoffeeScript lets you clearly express your program with a lot less code than JavaScript. It’s got a lot of sort of lightweight add-ons like Ruby style string interpolation and Python style list comprehension. It makes a lot of common tasks much easier than JavaScript. Pass around lot of functions, so CoffeeScript provides a very brief way of expressing those. It also brings a full class-based object system. JavaScript is generally prototypical, which confuses the hell out of most people. So that sort of class system familiar to Ruby and Python and Java developers makes things easier. JGB: Are there particular types of JavaScript development that would benefit from CoffeeScript? Paul: Personally, I use CoffeeScript for web development, game development and anything that I want to end up in JavaScript. Regardless of what libraries or platforms you’re building for, the output of CoffeeScript is JavaScript, so there’s nothing that it can’t do. JGB: Are there downsides to using CoffeeScript? Paul: The JavaScript served up to the browser is not the CoffeeScript that you write, so that can make runtime debugging more difficult. Personally, I haven’t found that to be a problem. The CoffeeScript compiler writes really clean, easy to read JavaScript, so I haven’t had a problem. There is an experimental technology called Source Maps and that’s said to make it even easier to do that runtime debugging. JGB: How do I get started with CoffeeScript, then? Do I need to install anything to use it? Paul: If you are using Rails 3.1 or newer, then CoffeeScript comes out of the box, so just enable the asset pipeline, put .coffee on the end of your file names, and Rails does the rest. If you’re using PHP or Django, or any other system — web, node, or otherwise — it’s easy enough to get up and running on your development environment. So, personally, I use Homebrew on my Mac to install Node.js and then use the node package manager, or NPM, to install CoffeeScript. That keeps a command line tool, which will turn your Coffee files into JavaScript.

Frequently Asked Questions about CoffeeScript

What is the main difference between CoffeeScript and JavaScript?

CoffeeScript is a programming language that compiles into JavaScript. The main difference between the two is that CoffeeScript has a more simplified and readable syntax compared to JavaScript. It removes the unnecessary punctuation and uses indentation to denote blocks of code, making it easier to write and read. However, it’s important to note that since CoffeeScript compiles into JavaScript, anything you can do in JavaScript can also be done in CoffeeScript.

Why should I use CoffeeScript instead of JavaScript?

CoffeeScript offers a more streamlined syntax, which can make your code cleaner and easier to read. It also includes some additional features not present in JavaScript, such as array comprehensions and destructuring assignments. However, whether you should use CoffeeScript over JavaScript largely depends on your personal preference and the specific requirements of your project.

How can I compile CoffeeScript into JavaScript?

You can compile CoffeeScript into JavaScript using the CoffeeScript compiler. This can be done through the command line by installing the CoffeeScript package via npm (Node Package Manager) and running the coffee command followed by the name of your CoffeeScript file. The compiler will then generate a JavaScript file with the same name.

Can I use CoffeeScript with Node.js?

Yes, you can use CoffeeScript with Node.js. In fact, CoffeeScript is often used in conjunction with Node.js due to its simplified syntax and additional features. To use CoffeeScript with Node.js, you’ll need to compile your CoffeeScript code into JavaScript first, as Node.js runs on JavaScript.

What are some of the key features of CoffeeScript?

CoffeeScript includes several key features that set it apart from JavaScript. These include a simplified syntax, array comprehensions, destructuring assignments, and the ability to write functions in a more concise manner. CoffeeScript also supports class-based inheritance, which is not natively supported in JavaScript.

Is CoffeeScript widely used in the industry?

While CoffeeScript is not as widely used as JavaScript, it is still used by a number of companies and projects. Its simplified syntax and additional features make it a popular choice for developers looking to write cleaner, more readable code.

How can I learn CoffeeScript?

There are many resources available for learning CoffeeScript, including online tutorials, books, and courses. The official CoffeeScript website also includes a comprehensive guide to the language, as well as a ‘Try CoffeeScript’ feature that allows you to write and compile CoffeeScript code directly in your browser.

Can I use CoffeeScript with front-end frameworks like React or Angular?

Yes, you can use CoffeeScript with front-end frameworks like React or Angular. However, you’ll need to compile your CoffeeScript code into JavaScript first, as these frameworks run on JavaScript.

Is CoffeeScript a statically typed or dynamically typed language?

CoffeeScript is a dynamically typed language, just like JavaScript. This means that you don’t need to specify the type of a variable when you declare it. The type of the variable is determined by the interpreter at runtime.

What is the future of CoffeeScript?

The future of CoffeeScript is uncertain. While it still has a dedicated user base, the popularity of JavaScript and the emergence of other compile-to-JavaScript languages like TypeScript have led to a decline in its use. However, CoffeeScript continues to be maintained and updated, and its simplified syntax and additional features continue to make it a viable choice for certain projects.

Jess Genevieve BrownJess Genevieve Brown
View Author

Jess is a video content creator for Learnable and Sitepoint. She digs all things social media, Web 2.0, music, film, digital media and innovation. Feel free get in touch!

CoffeeScript
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week