# Javascript Basics

Javascript's basic syntax is similar to Java's, despite being very different languages.

Besides trying out code in the Learneroo challenges, you can also use your browser's console. See Chrome help or Firefox Help.

``````
and end with star-slash */

// output will be shown with // =
1+1  // = 2

// Statements can be terminated by ;
doStuff();

// ... but they don't have to be, as semicolons are automatically inserted
// wherever there's a newline, except in certain cases.
doStuff()

// Because those cases can cause unexpected results,
// we'll keep on using semicolons in this guide.
``````

### Numbers

JavaScript has one number type (a 64-bit IEEE 754 double), with a 52-bit significand, which is enough to store integers up to about 9✕10¹⁵ precisely.

``````3; // = 3
1.5; // = 1.5

// All the basic arithmetic works as you'd expect.
1 + 1; // = 2
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
4 % 3 // = 1  //Modulus

// Including uneven division.
5 / 2; // = 2.5

// Bitwise operations also work; when you perform a bitwise operation your float
// is converted to a signed int *up to* 32 bits.
1 << 2; // = 4

// Standard order of operations with parentheses first.
(1 + 3) * 2; // = 8

// Use the Math object¹ for more advanced math:
Math.pow(2, 5);  // = 32
Math.sin(Math.PI / 2); // = 1

// There are three special not-a-real-number values:
Infinity; // result of e.g. 1/0
-Infinity; // result of e.g. -1/0
NaN; // result of e.g. 0/0
``````

1. See Javascript Math Docs

### Equality and comparisons

See below for Javascript's unusual equality checks.

``````
// boolean type.
true  // = true
false // = false

// Negation uses the ! symbol
!true; // = false
!false; // = true

// Comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Equality (recommended form)
1 === 1; // = true
2 === 1; // = false

// Inequality (recommended)
1 !== 1; // = false
2 !== 1; // = true

// With just two characters, == and != will do automatic type coercion:
"5" == 5; // = true
6 != "6" // = false
false == 0; // = true

// Which can lead to some unexpected results, so its use is discouraged:
'' == '0';  // = false
0 == '';   // = true  //??

// null and undefined
null; // used to indicate a deliberate non-value
undefined; // used to indicate a value is not currently present

// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.
// Note that 0 is falsy and "0" is truthy, even though 0 == "0".
!null // = true
!undefined // = true
!"0" // = false
!5 // = false

//Logical Operators: && (And), || (Or)
3>2 && 2>1; // true
2>3 && 2>1; // false
2>3 || 2>1; // true
``````

### Variables

``````
// Variables are declared with the var keyword.
// Javascript is dynamically typed, so you don't specify a type.
var someVar = "hello";
someVar = 5;

// if you leave the var keyword off, you won't get an error...
someOtherVar = 10;

// ...but your variable will be created in the global scope, which is not recommended.

// Variables declared without being assigned to are set to undefined.
var someThirdVar; // = undefined

// Shorthand for performing math operations on variables:
someVar += 5; // = 10  equivalent to: someVar = someVar + 5;
someVar *= 10; // = 100

// even-shorter-hand for adding or subtracting 1
someVar++; // now someVar is 101
someVar--; // back to 100
``````

### Challenge

What will the following return?

``````  2 + 3 / (1 + 1)
``````

Alternatively, you can try out Learneroo before signing up.

### Challenge

What will the following code return?

``````3 > 2 && 5 != "5"
``````

Alternatively, you can try out Learneroo before signing up.

### Challenge

ab is considered powerful if (and only if) both of the following 2 conditions are met:

1. ab >= 2 * b2
2. ab >= (a*b)2

return `true` if ab is powerful and `false` otherwise.