JavaScript and Java are similar in some ways but fundamentally different in some others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript follows most Java expression syntax, naming conventions and basic control-flow constructs which was the reason why it was renamed from LiveScript to JavaScript.
In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. JavaScript has a prototype-based object model instead of the more common class-based object model. The prototype-based model provides dynamic inheritance; that is, what is inherited can vary for individual objects. JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.
JavaScript is a very free-form language compared to Java. You do not have to declare all variables, classes, and methods. You do not have to be concerned with whether methods are public, private, or protected, and you do not have to implement interfaces. Variables, parameters, and function return types are not explicitly typed.
To get started with writing JavaScript, open the Scratchpad and write your first "Hello world" JavaScript code:
function greetMe(yourName)
{ alert("Hello " + yourName); }
greetMe("World");
Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, aliquam? Eum officia aperiam explicabo esse laboriosam tenetur quod molestiae autem eos optio. Quibusdam, debitis deserunt assumenda, non quisquam fugit veritatis eos porro molestias voluptatem ipsa. Ratione aut in rerum explicabo veritatis necessitatibus officiis fugiat. Dolore, soluta quibusdam? Natus, inventore sequi.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Facere, earum beatae amet quos consequuntur eos quis temporibus ab totam, natus voluptatem cupiditate dolorum necessitatibus. Deleniti eaque exercitationem ipsa? Aperiam modi, ratione ducimus odit doloremque omnis saepe ea impedit quasi qui et aut cupiditate quo itaque? Neque nostrum quo explicabo? Saepe, incidunt eligendi? Praesentium nisi ipsum vel saepe dolor, consequuntur nesciunt.
You can declare a variable in three ways:
With the keyword var. For example,
Var x = 42.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Asperiores velit ea incidunt fuga deserunt debitis temporibus consequuntur ipsam deleniti nobis doloremque qui, corrupti commodi tempora! Adipisci illum, ullam tempora.
This syntax can be used to declare both local and global variables.
x = 42.
laborum, mollitia voluptate ducimus sapiente accusantium natus unde, nesciunt distinctio ipsum perspiciatis iure placeat optio aliquam esse. Beatae ex blanditiis sequi veniam totam iste similique, aut cum dolor, amet qui expedita voluptatum dolore sapiente ut eligendi. Nisi ab blanditiis inventore ducimus.
This always declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.
With the keyword let. For example,
let y = 13.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat voluptatem id perferendis necessitatibus! Id repellendus architecto ut placeat, vitae temporibus sequi beatae reiciendis similique eligendi cumque maxime esse odit.
if (true) { var x = 5; } console.log(x); //5
aperiam eaque ratione fugit commodi illum accusamus quaerat autem repudiandae et labore? Possimus culpa, illum eius aperiam tempore qui similique sunt, voluptatem error officia animi libero officiis numquam deserunt neque ipsa at accusantium voluptatibus quia voluptatum aliquid exercitationem! Aspernatur tenetur placeat qui obcaecati itaque? Sunt aperiam quidem cupiditate, nam fuga magni.
Primitive data type
- Boolean. true and false.
- null. A special keyword denoting a null value. Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.
- undefined. A top-level property whose value is undefined.
- Number. 42 or 3.14159.
- Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.