JavaScript Syntax

In JavaScript, a value is any type of data that can be stored in a variable or constant. There are several types of values in JavaScript, including: Numbers, Strings, Boolean, null, undefined, Object, Symbol.

A variable or constant in JavaScript can hold any value, and can be reassigned to a different value at any time.

JavaScript Literals

In JavaScript, a literal is a notation for representing a fixed value in the source code. JavaScript literals are used to represent values such as numbers, strings, and boolean values directly in the code, rather than as variables or expressions.

There are several types of literals in JavaScript, including:

  • Numeric literals: Used to represent numbers, such as 42 or 3.14.
  • String literals: Used to represent strings of text, such as "hello world" or "John Doe". String literals are enclosed in single or double quotes.
  • Boolean literals: Used to represent the values true and false.
  • Array literals: Used to create an array, such as [1, 2, 3]
  • Object literals: Used to create an object, such as {name: "John", age: 30}
  • Template literals: These literals use `` (backticks) to define a string, these literals can contain expressions which will be evaluated and the result will be included in the string.

For example:

let num = 42; // this is a variable
console.log(42); // this is a numeric literal
console.log("Hello World"); // this is a string literal
console.log(true); // this is a boolean literal

In the above example, the first line creates a variable named num and assigns it the value 42. The next three lines use literals to directly print the values 42, "Hello World", and true to the console.

JavaScript Variables

In JavaScript, a variable is a container that stores a value. Variables are used to store data that can be accessed and modified throughout the program.

A variable is declared using the var, let, or const keyword, followed by the variable name. The variable name must start with a letter, underscore _, or dollar sign $ and can be followed by any number of letters, digits, underscores, or dollar signs.

For example:

var x; // declares a variable x
let y = 5; // declares a variable y and assigns it the value 5
const z = "hello"; // declares a constant variable z and assigns it the value "hello"

The var keyword is used to declare a variable and it function scope, which means the variable can be accessed within the function.

The let keyword is used to declare a variable with block scope, which means the variable can only be accessed within the block of code where it was defined.

The const keyword is used to declare a constant variable, which means the variable cannot be reassigned a new value once it has been declared.

You can use the assignment operator = to assign a value to a variable. Once the value has been assigned, it can be accessed or modified using the variable name.

For example:

var x = 10; // assigns the value 10 to the variable x
console.log(x); // prints the value of x to the console
x = 20; // reassigns the value of x to 20
console.log(x); // prints the new value of x to the console

In the above example, the first line declares a variable x and assigns it the value 10. The next line uses the variable name to print the value of x to the console, then the next line reassigns the value of x to 20 and the last line prints the new value of x to the console.

JavaScript Operators

In JavaScript, operators are special symbols or keywords that are used to perform operations on values, variables, and expressions.

There are several types of operators in JavaScript, including:

Arithmetic operators:

Used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. For example: +, -, *, /, % (modulus).

Comparison operators:

Used to compare values and evaluate to a boolean true or false. For example: == (equal to), === (strict equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).

Logical operators:

Used to combine and manipulate boolean values. For example: && (and), || (or), ! (not).

Assignment operators:

Used to assign a value to a variable. For example: =, +=, -=, *=, /=, %=.

Conditional (ternary) operator:

A shorthand for an if-else statement which takes a condition and return one of two expressions depending on whether the condition is true or false.

For example: (condition)? expression1 : expression2

typeof operator:

It returns a string which represents the type of the variable or expression passed to it. For example typeof variable or typeof (expression).

instanceof operator:

It returns a boolean value indicating whether an object is an instance of a particular constructor or not.

For example:

var x = 10;
var y = 5;
console.log(x + y); // prints 15
console.log(x == y); // prints false
console.log(x != y && x > 0); // prints true
x += 5; // x is now 15
console.log(x); // prints 15
console.log(typeof x) // prints "number"

In the above example, the first two lines declare variables x and y and assigns them values, then the next line uses the + operator to add x and y and print the result to the console.

The next line uses the == operator to compare x and y and print the result to the console.

The next line uses the != and && operator to check if x is not equal to y and x is greater than 0 and print the result to the console.

The next line uses the += operator to add 5 to x. The next line prints the new value of x to the console.

The last line uses the typeof operator to print the type of x to the console.

JavaScript Expressions

In JavaScript, an expression is any valid unit of code that can be evaluated to a value. An expression can be a single value, a variable, or a combination of values, variables, and operators that are combined to perform a specific operation.

For example:

5 + 10; // This is an expression that evaluates to 15
"Hello " + "World"; // This is an expression that evaluates to "Hello World"
x = 10; // This is an expression that assigns the value 10 to the variable x
x + y; // This is an expression that adds the values of x and y

Expressions can be used in various ways throughout JavaScript, such as:

  • Assigning the result of an expression to a variable
  • Passing an expression as an argument to a function
  • Using an expression as the condition in a control flow statement
  • Using an expression as a property or method of an object

Expressions can also be combined to create more complex expressions.

For example:

(x + y) * (z - 10); // This is a complex expression that combines multiple sub-expressions

Expressions are a fundamental building block of JavaScript, and are used in nearly every aspect of the language. Understanding how to use expressions effectively is essential for writing efficient and maintainable JavaScript code.

JavaScript Keywords

In JavaScript, keywords are reserved words that have a specific meaning and purpose in the language. They cannot be used as variable or function names.

There are several types of keywords in JavaScript, including:

  • Control flow keywords: Used to control the flow of the program. For example: if, else, switch, case, default, while, do, for, break, continue, return.
  • Data type keywords: Used to define the type of data being used. For example: var, let, const, function, class.
  • Access modifiers: Used to define the accessibility of the data or functions. For example public, private, protected.
  • Special keywords: Used for specific purposes. For example this, super, new, delete, in, instanceof, typeof.
  • Strict mode keywords: Used to enable strict mode in JavaScript. For example: "use strict".

For example:

if (x > 10) {
  console.log("x is greater than 10");
} else {
  console.log("x is less than or equal to 10");
}

function doSomething() {
  // function body
}

class MyClass {
  // class body
}

In the above example, if, else, function, and class are keywords. The if and else keywords are used to control the flow of the program, the function keyword is used to define a function, and the class keyword is used to define a class.

It's important to note that the JavaScript language is constantly evolving and new keywords may be added in the future versions, so it's important to keep an eye on the documentation.

JavaScript Identifiers

In JavaScript, an identifier is a name given to a variable, function, class, or property. Identifiers are used to represent the different elements in a program.

JavaScript has some rules for naming identifiers:

  • Identifiers must start with a letter, underscore, or dollar sign.
  • Identifiers can contain letters, digits, underscores, or dollar signs.
  • Identifiers are case-sensitive, meaning myVariable and myvariable are two different identifiers.
  • Identifiers cannot be a keyword or reserved word.

For example:

var x; // x is an identifier
let myVariable = 5; // myVariable is an identifier

function doSomething() { // doSomething is an identifier
  // function body
}

class MyClass { // MyClass is an identifier
  // class body
}

In the above example, x, myVariable, doSomething, and MyClass are identifiers. They are used to represent the different elements in the program: a variable, a variable with an assigned value, a function, and a class.

JavaScript also supports using Unicode characters in the identifier names, also called Unicode Identifiers, which allows developers to use non-latin characters in their identifier names, such as Greek letters, Chinese characters, and so on.

However, it's important to keep in mind that not all environments support Unicode identifiers, and it's recommended to use them only when it's necessary, and make sure that the code will be readable by other developers.

Is JavaScript Case Sensitive?

Yes, JavaScript is a case-sensitive programming language. This means that the language treats variables, functions, and other identifiers differently depending on whether they use uppercase or lowercase letters.

For example, the variable myVariable is different from the variable myvariable, and the function doSomething() is different from the function doSomething().

It's a common practice in JavaScript to use camelCase for variable and function names, where the first word is in lowercase and the first letter of subsequent words is capitalized, and use PascalCase for class names, where the first letter of each word is capitalized. This helps to make code more readable and maintainable.

It's important to keep in mind that JavaScript is case-sensitive, and be consistent when naming variables, functions, and other identifiers in your code, to avoid any confusion or errors.

JS Basics