Javascript

Page content

JavaScript Guide

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

Comments

// a one line comment

/* this is a longer,
 * multi-line comment
 */

Declarations

# Declare variable, optionally initialize it to a value
var a = 1;

# Declares a block-scoped, local variable, optionally initializing it to a value.
let b = 2;

# Declares a block-scoped, read-only named constant.
const c = 3;

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (0–9). JavaScript is case sensitive, var a != var A …

Example Vars: Number_hits, temp99, $credit, and _name.

Data Types

Seven data types that are primitives:
* 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 not defined.
* Number. An integer or floating point number. For example: 42 or 3.14159.
* BigInt. An integer with arbitrary precision. For example: 9007199254740992n.
* String. A sequence of characters that represent a text value. For example: "Howdy"
* Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.
and Object

Converting strings to numbers

  • parseInt()
  • parseFloat()
'1.1' + '1.1'       // '1.11.1'
(+'1.1') + (+'1.1') // 2.2
+'1.1' + +'1.1'     // 2.2
+'1.1'+ +'1.1'      // 2.2
// Note: the parentheses are added for clarity, not required.

Literals

Literals represent values in JavaScript. These are fixed values—not variables—that you literally provide in your script. This section describes the following types of literals:

* Array literals
* Boolean literals
* Floating-point literals
* Numeric literals
* Object literals
* RegExp literals
* String literals

Array literals

let coffees = ['French Roast', 'Colombian', 'Kona'];

Boolean literals

The Boolean type has two literal values: true and false.

Numeric literals

JavaScript numeric literals include integer literals in different bases as well as floating-point literals in base-10.

Integer literals

Integer and BigInt literals can be written in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

0,      117,      123456789123456789n       (decimal, base 10)
015,    0001,     0o777777777777n           (octal, base 8)
0x1123, 0x00111,  0x123456789ABCDEFn        (hexadecimal, "hex" or base 16)
0b11,   0b0011,   0b11101001010101010101n   (binary, base 2)

Floating-point literals

A floating-point literal can have the following parts:

  • An unsigned decimal integer,
  • A decimal point ("."),
  • A fraction (another decimal number),
  • An exponent.
3.1415926
.123456789
3.1E+12
.1e-23

Object literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

var sales = 'Toyota';

function carTypes(name) {
  if (name === 'Honda') {
    return name;
  } else {
    return "Sorry, we don't sell " + name + ".";
  }
}

var car = { myCar: 'Saturn', getCar: carTypes('Honda'), special: sales };

console.log(car.myCar);   // Saturn
console.log(car.getCar);  // Honda
console.log(car.special); // Toyota

RegExp literals

var re = /ab+c/;

String literals

'foo'
"bar"
'1234'
'one line \n another line'
"John's cat"

a='one line \n another line'
console.log(a)
one line 
 another line

var name = 'Bob', time = 'today';
console.log(`Hello ${name}, how are you ${time}?`)
Hello Bob, how are you today?

sha256: 551e993b0308034b85726764a4d82274d7c3923cfebe910719d0448742d1f6ee