Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Recently, in a job interview, I was asked what the difference between Python and Javascript is.

The interviewer had framed the question as:

Javascript is an interpreted language which runs in a web browser and Python runs in its own interpreter, but how are they different?

I tried giving some differences, but I was told that my answer is incorrect.

Do you have any idea on what the correct answer to this question would be?

EDIT: I am sorry if I was unclear with my question. He wanted a fundamental difference between the languages such as, C is compiled but JavaScript is interpreted. The answer he wanted was of that level.

My original answers were:

  1. You can do system level programming with Python but not with Javascript
  2. You can compile Python but not JavaScript
  3. Javascript can be used for formatting webpages, but Python cannot
share|improve this question
25  
It's the classic "What am I thinking?" interview question!!! I've failed several interview hurdles by giving technically correct, but not what the wanted questions. It's even more fun when the interviewer is an HR drone, rather than a technical lead who could recognize that you know what you're talking about. – Chris Cudmore Sep 13 '11 at 16:39
4  
why don't you ask the person that was expecting you to read their minds, because none of us are mind readers here either... – Jarrod Roberson Sep 13 '11 at 16:45
17  
For what it is worth, the question itself was technically incorrect. To say that JavaScript is interpreted and runs in a browser is as accurate as saying that English is a language spoken 100 years ago in Teaneck, NJ. Also, Python doesn't run in an interpreter; it runs in a Virtual Machine. You were declared wrong by a fool. Wear that as a badge of pride. – Adam Crossland Sep 13 '11 at 16:49
4  
@Akash -- one could literally spend most of a day enumerating not only the differences between JS and Python but also their similarities as well. As chris and Jarrod already pointed out, this is a mind-reading question. – Adam Crossland Sep 13 '11 at 16:56
3  
@Erik that didn't work in this case, all his answers were fundamentally wrong, more wrong information would have just put more nails in the coffin. – Jarrod Roberson Sep 13 '11 at 19:09
show 8 more comments

closed as not a real question by gnat, Glenn Nelson, GlenH7, MichaelT, Eric King Feb 7 at 14:53

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

14 Answers

JavaScript was written in a few weeks and python was developer over a period of a few years.

"the correct answer" doesn't exist. The differences are vast and numerous. Pick whatever fact you want.

JavaScript was written by Brendan Eich & python was not.

Python has significant whitespace, JavaScript doesn't.

It's a trick question really, I presume they just want some high level understanding of JavaScript or python. Your answer cannot be "incorrect" as long as you have your facts right.

share|improve this answer
Actually, javascript has significant whitespace as well. Most languages do. For example, the space in a++ +b is significant. Another example is any whitespace after a backslash intended for line continuation in string literals. Obviously it has a lot less than python, but it's there. – Alex Feb 7 at 12:56
@Alex not to mention that JS has rules for when an EOL can terminate a statement without requiring a semicolon. – Sean McSomething Feb 7 at 18:13

Among other things, the languages are very different syntactically. There is a lot of room for compare and contrast in how things work in the language.

You could also delve into the domains of the language and how they are the right fight for their given areas. In addition you could talk about the problems with how they work and limitations as well.

share|improve this answer

I think that he either wanted to hear that Java_Script_ is a scripting language, where Python can be a programming and scripting language, or he wanted to hear that JavaScript is weak typed but Python is strong typed.

Tbh, I'd have seriously answered with "JavaScript runs in web browsers and Python runs in its own interpreter" or what he wants me to say, since there are many different things. What did you actualy answer which was wrong?

share|improve this answer
2  
Both of those are meaningless though. Javascript and Python's type systems are both dynamically typed (and very similar.) And Programming vs Scripting has very little to do with the language, and everything to do with the manner in which it is used. – Sean McMillan Sep 13 '11 at 17:06
@Sean: If those type systems are similar, so are the type systems of C++ and Go. Python has classes with bells and whistles like metaclasses, descriptors and multiple inheritance where Javascript has a comparatively simple prototype-based object system. Python has few implicit conversions while they're nearly impossible to avoid in Javascript. – delnan Sep 13 '11 at 17:09
@delnan Fair enough, but I wouldn't call that weakly and strongly typed, which are usually euphemisms for statically and dynamically typed. Classes vs Prototypes is a notable difference. – Sean McMillan Sep 13 '11 at 17:16
@Sean: To nitpick even further... Weak/strong typing is frequently (at least as frequently, if not more frequently) taken to be orthogonal, see various questions stackoverflow.com/questions/tagged/weak-typing, but apart from that it's ill-defined, so I generally avoid it (and I did in my above comment as well, so I wonder why your comment seems to say otherwise?). – delnan Sep 13 '11 at 17:19
2  
@denlan: Sorry, the Answer above says python is strongly typed and js is weakly typed. I think that's a bad/wrong answer. I followed that line into the comments here. – Sean McMillan Sep 13 '11 at 17:27

My original answers were

  1. You can do system level programming with Python but not with Javascript
  2. You can compile Python but not javascript
  3. Javascript can be used for formatting webpages, but Python cannot

your original answers are all wrong, maybe it that is why you didn't get that question correct, your knowledge of the architectures of the two languages is incorrect or incomplete.

  1. You can do the same tasks in Python at the OS System level you can do with Javascript ( SpiderMonkey ). You can extend or enhance both languages and runtimes with C extensions to do low level OS System calls very easily.

  2. Python "compiles" to byte code, the same thing is done with JavaScript, it depends on the implementation of the runtime. Rhino which is a Java runtime for JavaScript compiles to Java byte code. All Browser implemenations of JavaScript have JIT in time compilers that compile the JavaScript to byte code at runtime and then continually optimize that byte code using the heuristics found during execution. Which results in faster code than statically compiled code.

  3. Python is a Turing complete language, it can be used to any task that any other language can do, formatting web pages is just text generation, in many cases easier than JavaScript can depending on what frameworks or libraries you employ. Technically CSS is used to format web pages, if you are doing in JavaScript you are probably doing it wrong.

NOTE: There are many implementations of Python and of ECMAScript/JavaScript. All of them vary wildly in their implementation details and in some cases have more in common with each other than differences. Jython and Rhino both run on the Java Virtual Machine for example, that means the syntax is the only real difference.

share|improve this answer
Yes, I realise that my answers were wrong(Thats one of the reasons I hadnt posted them initially), but was just looking for some correct ones – Akash Sep 13 '11 at 17:06
This is an answer I can agree with. – Benjamin Gruenbaum Feb 24 at 1:01

These sorts of open-ended questions don't have a "correct" answer, and I am surprised the interviewer told you directly that your answer was incorrect. It's possible the interviewer was unqualified to ask the question, or just had little experience interviewing candidates.

If I had asked that question and gotten your answers, I would have probed a bit deeper, saying "Yes, they are typically used in different ways, but how do the languages differ?" Then I would have hoped to hear about the differences in OO support (JavaScript prototypes vs. Python inheritance), functional programming support, etc.

Still, in my opinion, none your statements:

  • You can do system level programming with Python but not with Javascript
  • You can compile Python but not Javascript
  • Javascript can be used for formatting webpages, but Python cannot

are correct:

  • You can do system-level programming in Javascript (see node.js)
  • Any language can be compiled. Just because you don't have to invoke a compiler to run Javascript doesn't mean it can't be compiled.
  • Python could be used for formatting webpages, and often is on the server side. Python could also run on the client side; there is a Python compiler that produces Javascript.

I ask these sorts of questions to see how deeply the candidate has thought about the programming process. I prefer candidates with well-reasoned opinions about the technologies they have used. If a candidate has used two languages extensively, I would expect them to be able to discuss their likes and dislikes to some depth. Otherwise I conclude that the candidate has only a superficial understanding of the languages, and has mostly been programming at the search-and-paste level.

BTW, memorizing better answers to this question won't help you much in future interviews at similar companies. Interviewers use these open-ended questions to evoke a technical discussion. If you had given more correct answers, the interviewer would probably have followed up asking for more detail. If you express a preference for Python over Javascript or vice versa, a good interviewer will ask you to explain and defend your preference.

share|improve this answer
What is your definition of "system-level" programming? Because I'd have said you can't do it in either language, or any language that doesn't have pointers for that matter. – Mason Wheeler Sep 22 '11 at 22:47
2  
@Mason: I wasn't thinking of kernel-mode code, like thread scheduling and device drivers. About everything you can do with a user-mode C application could be done in Python or Javascript. – kevin cline Sep 22 '11 at 23:24
1  
+1: also worth mentioning that Firefox and IE support Python as scripting language using <script type="text/python">, Fx natively, IE through .NET/DLR extension. Of course only for trusted code (webpage out on the internet is not trusted). Apart of that there is PyJS (aka Pyjamas) – vartec Feb 7 at 14:57

The biggest difference that leaps out at me is that JavaScript does inheritance very differently than Python (ie. not through classes).

However this is only the first thing that comes to mind; there are lots of differences and, as others have pointed out, this question is a mind-reading exercise. I wouldn't have a problem with a question like this if the interviewer coaches you along in the direction he wants, but if he simply went "EEEHHHH wrong, next question!" then that's awful.

share|improve this answer

Both are object oriented. JavaScript has no built-in class features but is flexible enough to accommodate the approach.

JS inheritance is prototypal. It is designed more around object factories than cascading layered-class inheritance schemes (which are an antipattern in any language, IMO).

JS is definitely more arguably a functional language. It is in fact inspired primarily by Scheme making it a massively heavily functional-based language, which is something that surprises a lot of devs that still think of it as a toy language or functional programming as something shiny and new.

Python's lambda functions might make it a functional language technically speaking depending on whose definition of functional you're using but that's more of a bolted-on kind of a thing.

JS syntax was meant to be easy for Java developers to adapt to (hilarious since JS design tends to be seen as heresy to most Java devs) and is c-based.

Python's syntax is more its own thing and relies on colons and whitespace to define blocks and carriage returns to define statement separation.

Python is more evolved as a glue language whereas JS has just started being used outside of the client-side web. Python has bindings and APIs that plug it into damn near everything. JS is only just starting to touch file systems through independent JIT-compilers like Google's V8 engine and libraries like Node.js

Forgot to Add: Both are awesome

share|improve this answer
3  
To say that JS is more functional than Python demands some specifics. Both have first-class functions and closures. Both support higher-order functions and recursion. What else? But to say that JS is a 'massively heavily functional-based language' seems to be hyperbole to be. Haskell and Cloure? Sure. JavaScript? No. – Adam Crossland Sep 13 '11 at 17:49
3  
JS has a functional programming foundation down to the core of its design. Brendan Eich was following Scheme as his primary inspiration when he wrote it. JS functions (all JS functions) bind an execution context and can have values assigned to them because they are also data structures. Python Lambdas are an add-on. I think they probably do technically qualify Python as functional. I also love both languages, but if we consider 'functional' as a scale rather than a boolean, I would say JS is more functional-oriented than Python 2.7 (haven't used 3 yet). – Erik Reppen Sep 13 '11 at 18:37
   
@Erik, lambdas have nothing to do with whether or not Python is functional. Python's functions are first-class objects just like Javascript's, and can also have values assigned to them and create closures. The main difference seems to be that anonymous functions (lambdas) are much more limited in Python, but that isn't enough to determine whether or not a language is "functional". IMO, neither JS nor Python are functional. – Daniel Roseman Sep 16 '11 at 15:07
2  
JS is "definitely more arguably" a functional language? Umm...? – Mason Wheeler Sep 22 '11 at 22:48

The security model is radically different - Python can call native C/Assembly libraries even though it is interpreted and run from within a virtual machine, whereas Javascript only runs in a jail (the frame). Thus Python scripts can access hardware whereas Javascript is fully abstracted from the hardware by design (extensions to this effect are proprietary and non-standard).

share|improve this answer
1  
That's if both are run in their usual runtime. There are sandboxed Python interpreters and at least one JS implementation (Node) allows filesystem access and probably more. – delnan Sep 13 '11 at 17:38
It doesn't take different JS implementations into account but it's not a -vote worthy bad answer. + for balance. But yeah, it's getting less and less okay to only talk about JS from the perspective of the browsers which would also put blocks on Python if it were to one day become a client-side scripting language. – Erik Reppen Sep 13 '11 at 18:23
1  
Down voted because Javascript can be run outside a sandbox. – Jim In Texas Sep 22 '11 at 21:04
.. except no one does. – Jonathan Cline IEEE Sep 22 '11 at 22:54

There are no good answers to bad questions.

Javascript is an interpreted language which runs in a web browser and Python runs in its own interpreter, but how are they different?

is just a bad question.

share|improve this answer

My guess would be the interviewer was looking for a deeper understanding of the two languages such as (from the introduction to Bruce A. Tate. Seven Languages in Seven Weeks):

  • What is the typing model? Typing is strong (Java) or weak (C), static (Java) or dynamic (Ruby). The languages in this book lean on the strong typing end of the spectrum, but you'll encounter a broad mix of static and dynamic. You will find how the trade-offs impact a developer. The typing model will shape the way you attack a problem and control the way the language works. Every language in this book has its own typing idiosyncrasies.
  • What is the programming model? Is it object-oriented (OO), functional, procedural, or some type of hybrid? This book has languages spanning four different programming models and, sometimes, combinations of more than one. You will find a logic-based programming language (Prolog), two languages with full support for object-oriented concepts (Ruby, Scala), four languages that are functional in nature (Scala, Erlang, Clojure, Haskell), and one prototype language (Io). Several of the languages are multiparadigm languages, like Scala. Clojure's multimethods will even let you implement your own paradigm. Learning new programming paradigms is one of the most important concepts in this book.
  • How will you interact with it? Languages are compiled or interpreted, and some have virtual machines while others don't. In this book, I'll begin to explore with an interactive shell, if there is one. I will move on to files when it's time to attack bigger projects. We won't attack large enough projects to fully dive into packaging models.
  • What are the decision constructs and core data structures? You'd be surprised how many languages can make decisions with things other than variations of ifs and whiles. You'll see pattern matching in Erlang and unification in Prolog. Collections play a vital role in just about any language. In languages such as Smalltalk and Lisp, the collections are defining characteristics of the language. In others, like C++ and Java, collections are all over the place, defining the user's experience by their absence and lack of cohesion. Either way, you'll need a sound understanding of the collections.
  • What are the core features that make the language unique? Some of the languages will support advanced features for concurrent programming. Others provide unique high-level constructs such as Clojure's macros or Io's message interpretation. Others will give you a supercharged virtual machine, like Erlang's BEAM. Because of it, Erlang will let you build fault-tolerant distributed systems much more quickly than you can in other languages. Some languages support programming models that are laser-focused on a particular problem, such as using logic to solve constraints.

Bruce A. Tate. Seven Languages in Seven Weeks (John Gaines, Jr.) (Kindle Locations 148-165). The Pragmatic Bookshelf (290307).

share|improve this answer
+1: these would be the answers i would expect too. – keppla Sep 23 '11 at 6:59

Python has a very rich data structure library and helper functions to go with it. I am talking lists with list-comprehensions, dictionaries, tuples, queues, deques, etc.
JavaScript doesn't have any of these. It only has an Object and an Array (which is more like an object anyway).

share|improve this answer

Odd that I've been doing 'system level programming' (which I take to mean calling in to the host OS API) in Actionscript (aka 'Javascript' with a class library) for a while now. I'm sorry to hear that I can't do what I've been doing.

Even without the Actionscript library Javascript can still call a C style OS API call, hence can be used for 'system level programming'.

The HR drone doesn't understand that Javascript is just a language that will run in lots of places besides just in a web page sandbox.

share|improve this answer
Why would you use actionscript without flash. Surely there are more stable scripting language for talking to the OS. – Raynos Sep 22 '11 at 22:15
@ Raynos - Because I'm dealing with more than one OS. – Jim In Texas Sep 25 '11 at 18:33

I think the best answer was:

  • Javascript is an interpreted language which runs in a web browser and Python runs in its own interpreter, but how are they different?

They are not so different. In fact, their design are theorically VERY close. Furthermore, if you ask me to name two very close languages, I will answer Javascript(actionscript/ecmascript) and Python.

  • So what?

Practically, they are used in very different situations. As you said, javascript is mostly used in Web browsers as a DOM scripting language, whereas Python is used for... everything but Web browsers.

share|improve this answer

The main difference is that JavaScript isn't object oriented, it's a Prototype-based language.

Python is also an indented language. :)

However, your answers are wrong, Sites and webapps can be and are heavily formatted using Python, infact - it's more correct to do so, SEO-wise than on JavaScript.

JavaScript can be used to write anything, node.js is just one example which is based on common.js, which basically run V8 google chrome interpreter which can be used to do just about anything.

As for compilation, I assume you can create a compiler to compile to another high-level compiled language, such as C and compile them both, HipHop by facebook is a good example of dynamic language compiler (php in this case).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.