Expressions in JavaScript

In any programming language, there’re certain entities which come together to make code empowered to run on a machine. Similarly JavaScript does have a number of these unique entities. One of these is Expression which is an entity that is evaluated to a value by JavaScript Engine.

Moreover in JavaScript even a simple variable is an expression, whose value evaluates to something which it was assigned. In the same way, multiplication operation is also an expression x * y.

There’re many types of expressions in JavaScript, let’s discuss each one by one.

Primary Expressions

Simplest expressions in JavaScript are known as Primary Expressions. Like constant values, JavaScript reserved keywords and variable references. Despite all of these, literals are also consider Primary Expressions in JavaScript.

  • 10.4923 //Number literal
  • “Computer Science Hub” //String literal
  • true //JavaScript reserved keyword
  • false //JavaScript reserved keyword
  • i, j //Any variable in JavaScript is also considered Primary Expression

Also whenever JavaScript Engine while executing code finds an identifier then it look up into code for its value, if in case it doesn’t find value of identifier. Then it would through a ReferenceError.

Array Initialization Expressions

In javaScript initialising an array is super easy simply just use two square-brackets and put data values inside those with each value being separated by a comma. For example – [] is an empty array whilst [1,2,3] is an array with three elements.

Whenever JavaScript Engine encounters an entity in code which have two square brackets, then it treats that as an Array Expression. And accordingly parse values inside it.

Object Initialisation Expressions

In JavaScript, an object is just a bundle of property, value pairs. These pairs when put together inside curly brackets define an Object, this expression is known as Object Initialisation Expression.

If you’re not aware of what Objects are in JavaScript then you don’t need to worry at all, as I’ve put together awesome information about that as well on this website – Objects in JavaScript.

Here are some examples of Object Initialisation Expressions which would you better understand these –

  • var John = { age: 10, height: 1.2} //John object with two properties age, height
  • var fun = {} //An empty object initialisation expression

Defining Functions Expressions

In JavaScript a Function is a just a block of code, which when called does certain action/computation and spit out result. Practically there does exist many ways to define functions in JavaScript, but personally I use Function Expression. Because most of time, I want to capture function as a simple variable for latter use in the code.

var addition = function(a, b) { return a + b; };
Function expression capturing addition as a variable ‘addition’ for latter use in code

Property Access Expressions in JavaScript

Property access expressions evaluated to value of an object property. For example – If we have an object var man = {age:10, height:5.6} then expression man.age will evaluate to 10, and this expression would be called Property Access Expression.

Doing like man.age is just a one way of accessing object’s property value, other way could be man[“age”]. Both of these will result in same value, it’s just difference of syntax. Use whichever you want, while writing JavaScript Code.

Ways of accessing property value of an object
expression.identifier
expression[identifier]

Invocation Expressions in JavaScript

JavaScript syntax for calling/executing a function/methods is Invocation Expression, it often begins with function expression which identifies function itself.

Examples of Invocation Expressions
f(100)
[1,10, 201].sort()

Note that an Invocation Expressions will only return a value, if innovated object/function have a return statement. That returned value will be spitted out by Invocation Expression.

Object Creation Expressions in JavaScript

Creating an object often involves two steps in programming – creating empty object then assigning it certain parameters/values. This is kind of common Object creation procedure used by most of well known programming languages.

In the same way, JavaScript’s object creation expression first creates a new object then invokes a constructor function for initialising properties of that object.

JavaScript uses new keyword along with some class function for making objects.

new Object()
new Laptop(“Apple”, “2018 Model”)

Here is the procedure which JavaScript follows for creating Objects –

  1. Firstly JavaScript creates an empty object
  2. Next it invokes specified function with specified arguments
  3. Pass empty object as value of this JavaScript keyword

This procedure for object creations through expressions seems weird, let’s try to understand this with an example – If we have expression new Laptop(“Apple”, “2018 Model”).

  1. JavaScript would create an empty Laptop() object
  2. Then it will call Laptop() function passing parameters “Apple”, “2018 Model”
  3. When JavaScript would get an object returned from Step 2 then it would assign it as this property of empty object created in Step 1.

This seems kind of difficult to understand, but don’t worry it also took me sometime before I could clearly understand how this works. Moreover as your experience grows then your ability to just imagine this object creation visually in mind would also improve. So take it easy, if you not understand this at the moment.

Arthimetic Expressions in JavaScript

Artimetics is Math of doing addition, subtraction, multiplication and division. JavaScript uses simple operators for these as does other programming languages. Here are some examples –

1 + 2 // returns 3
1*2 // returns 2
“Computer” + ” ” + “Science” + ” ” + “Hub” // returns Computer Science Hub
“10” + “20” // returns 1020

Relational Expressions in JavaScript

Expressions used for comparing two identifiers in JavaScript are Relation Expressions, in terms of simple Maths these are “equals”, “more than”, “less than” and so on. Often the value evaluated from Relational Expressions is Boolean either true or false. Let’s see some examples –

  • 1 > 10 // returns boolean false
  • 100 > 30 // returns boolean true

Logical Expressions in JavaScript

Logics is very crucial part of any programming language, because coding is essentially making decisions here and there to get to final result. If your beginner then you may not be aware of how much decisions need to be made in code. For doing this decision making most of programming languages use Logical Expressions &&, ||, ! are some of common ones.

if ((a || b) == 10) {
Learn programming from ComputerScienceHub.io
}
else {
Learn it from other sources
}

JavaScript Assignment Expressions

In JavaScript variables can be declared either individually or can be assigned some value at the same time. So saying like var a; or var a = 10; are both legal statements in JavaScript.

var a = 10; //JavaScript Assignment Expression

Conclusion

This is all about JavaScript Expressions, if you’ve any question about these or want to ask something. Then definitely comment down below, I would try to reply as soon as possible.

In case your learning JavaScript, then I’ve put together a number of articles covering various aspects of JavaScript on this website. You can check those out here – JavaScript Learning Computer Science Hub.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts