Home » Learn » Javascript

Basic JavaScript Syntax

 · 5 min · Nasir

This module will cover how variables, data types, operators, control structures and functions are used in JavaScript.

Learn JS

JavaScript is a popular programming language that is widely used on the web. It is known for its simplicity and flexibility, making it a great choice for beginners. In this article, we will take a look at some of the basic syntax of JavaScript.

JavaScript is a case-sensitive language, which means that the capitalization of your code matters. It is also a loosely-typed language, which means that you don’t have to specify the data type of a variable when you declare it.

Variables

In JavaScript, variables are used to store data. To declare a variable, you use the var keyword followed by the name of the variable. For example:

var myName;

You can also assign a value to a variable when you declare it:

var myName = "John";

There are also two other keywords that you can use to declare variables: let and const. The let keyword is similar to var, but it has a more limited scope. The const keyword is used to declare variables that cannot be re-assigned.

Data Types

JavaScript has several data types, including:

  • String: A sequence of characters, enclosed in quotes. For example: "Hello, World!"
  • Number: A numeric value. Can be an integer or a floating-point number.
  • Boolean: A value that is either true or false.
  • Array: An ordered list of values. Arrays are enclosed in square brackets and the values are separated by commas. For example: [1, 2, 3]
  • Object: A collection of key-value pairs. Objects are enclosed in curly braces and the key-value pairs are separated by commas. For example: { name: "John", age: 30 }

Operators

JavaScript has several operators that you can use to perform operations on your data. Some common operators include:

  • + Used to add two values.
  • - Used to subtract one value from another.
  • * Used to multiply two values.
  • / Used to divide one value by another.
  • % Used to find the remainder when one value is divided by another.

Control Structures

JavaScript has several control structures that you can use to control the flow of your code. These include:

  • if/else: Used to execute a block of code if a certain condition is true, or a different block of code if the condition is false.
  • for: Used to execute a block of code a specific number of times.
  • while: Used to execute a block of code as long as a certain condition is true.

Functions

In JavaScript, functions are blocks of code that can be executed when they are called. They are a powerful feature of the language that allow you to reuse code and make your programs more modular and organized.

Declaring and calling a function

To declare a function in JavaScript, you use the function keyword followed by the name of the function, and then enclose the code that you want to execute in curly braces. For example:

function sayHello() {
  console.log("Hello, World!");
}

You can then call the function by using its name followed by parentheses:

sayHello(); // Outputs "Hello, World!"

Passing parameters to a function

You can also pass parameters to a function, which are values that are passed to the function when it is called. For example:

function sayHello(name) {
  console.log("Hello, " + name + "!");
}

sayHello("John"); // Outputs "Hello, John!"

Inside the function, you can use the parameters like any other variables.

You can also return a value from a function using the return keyword. For example:

function add(x, y) {
  return x + y;
}

console.log(add(2, 3)); // Outputs 5

Ways to define functions

Functions can be defined in different ways in JavaScript, such as function expressions and arrow functions.

Function Expression

Function expressions are created by assigning a function to a variable:

const add = function(x, y) {
  return x + y;
};

console.log(add(2, 3)); // Outputs 5

Arrow Function

Arrow functions are a shorthand syntax for defining functions. They are defined using the => syntax:

const add = (x, y) => {
  return x + y;
};

console.log(add(2, 3)); // Outputs 5

Overall, functions are a crucial part of JavaScript and are used extensively in modern programming. They allow you to organize your code, reuse code, and create more powerful and flexible programs.

Summary

JavaScript is a popular programming language that is widely used on the web. It is known for its simplicity and flexibility, making it a great choice for beginners.

In this article, we covered some of the basic syntax of JavaScript, including:

  • Variables: In JavaScript, variables are used to store data. You can declare a variable using the var, let, or const keywords.
  • Data types: JavaScript has several data types, including strings, numbers, booleans, arrays, and objects.
  • Operators: JavaScript has several operators that you can use to perform operations on your data, such as +, -, *, /, and %.
  • Control structures: JavaScript has several control structures, such as if/else, for, and while, that allow you to control the flow of your code.
  • Functions: Functions are blocks of code that can be executed when they are called. They allow you to reuse code and make your programs more modular and organized.

Overall, these are just a few of the basic concepts of JavaScript syntax. There is much more to learn, but understanding these foundations will give you a good start in your journey to becoming a JavaScript programmer.

We use cookies to improve your experience on our site and to show you relevant advertising.

Read cookie policy

Accept & Close