Javascript Grammar And Types

Basics

JavaScript borrows most of its syntax from Java, C and C++, but is also influenced by Awk, Perl and Python.

Comments

The syntax of comments is the same as in C++ and in many other languages:

Declarations

There are three kinds of declarations in JavaScript.

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

Declaring variables

You can declare a variable in two ways:

  • With the keyword const or let. For example, let y = 13. This syntax can be used to declare a block-scope local variable. See Variable scope below.

Evaluating variables

A variable declared using the var or let statement with no assigned value specified has the value of undefined.

Variable scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

Variable hoisting

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense “hoisted” or lifted to the top of the function or statement. However, variables that are hoisted return a value of undefined. So even if you declare and initialize after you use or refer to this variable, it still returns undefined.

Function hoisting

For functions, only the function declaration gets hoisted to the top and not the function expression.

Global variables

Global variables are in fact properties of the global object. In web pages, the global object is window, so you can set and access global variables using the window.variable syntax.

Constants

You can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign ($) and can contain alphabetic, numeric, or underscore characters.

Data structures and types

Data types

The latest ECMAScript standard defines eight data types:

  • 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

Data type conversion

JavaScript is a dynamically typed language. That means you don’t have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. So, for example, you could define a variable as follows:

Converting strings to numbers

In the case that a value representing a number is in memory as a string, there are methods for conversion.

  • parseFloat()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store