Javascript, or popularly JS, is a text-based programming language. It is popularly used both for front-end and backend programming for client-side and server-side programming. The language is common;y used to impart an interactive element that allows the users to engage with the web pages. In addition, the language is used for updating content, controlling multimedia, and also for animating images.
Javascript allows for creating dynamic and interactive pages by web developers. It is an easy-to-learn language even if you don’t have much coding or programming knowledge and is quite beginner-friendly. Here is a Javascript programming cheat sheet for beginners to help you learn more about the language.
The main codes and data types you should know about JavaScript are:
You use console.log() to print messages on the console or log messages. It is also used to print objects.
Example:
console.log('Hello World!'); This code snippet prints the message: Hello World!
Strings are the most common data type used in JavaScript and can be understood as a string of characters, letters, spaces, numbers, or symbols. A string is always enclosed with single ( ‘ ‘ ) or double ( ” ” ) quotation marks.
Example:
let single = 'This is an example!'; let double = "Here is an example!";
Numbers in JavaScript can contain both integers and decimals in floating-point numbers. Hence, you can write whole numbers or fractions.
Example:
let amount = 10; let price = 9.99;
Boolean data types come in only two values. Either variable name can be ‘true’ or ‘false. They are one of the most commonly used data types in JavaScript.’
Example:
let anExample= true;
Null can be described as a data type where a variable is intentionally made to be absent of any value. Thus, the used data type is ‘null’.
Example:
let x = null;
You can assign these data type values by using ‘=.’
The common arithmetic operations carried by Javascript are:
When you want to add Javascript to a web page, you use the script <> tags.
Syntax example:
<script type="text/javascript"> //here you write the Javascript code </script>
Comments, though not strictly part of the code, help break down your programming code into easily decipherable text that helps you or others who will be seeing your code to understand what a particular snippet stands for or what is going behind the scenes. In a sense, they are notes for your program so that you can remember why a certain code is in the way it is.
However, you can not simply type comments along with a code statement just as you would do while typing some text. Instead, you need to properly assign comments so that they are not executed when you run your program. The two types of comments are:
Single line comments: These are marked as ‘comment’ with double slashes ( // ) and should only be used when you want to write a small, precise comment.
Long comments: If your comment is of multiple lines and you do not want to execute it, you need to wrap it with /* and */ to mark it as a comment.
A library in Javascript has pre-created methods that can instantly be called upon to carry some function.
Syntax: To call a library, first write the library name, follow it with a period ( . ) and then with the method, and finally the parenthesis.
For example:
Math.random();
Here, Math is the library with methods like ‘random’ in it.
Examples: Math.random()
The Math.random() function, when called, executes to print a random floating-point from 0 to 0.9.
Example:
console.log(Math.random()); // Prints: 0 - 0.9
Math.floor()
The Match.floor() function is used to print the largest integer, which is less than or equal to the number provided.
Example:
console.log(Math.floor(7.95)); // Prints: 7
Variables can be understood as non-fixed values, which can be assigned values at the time of carrying operations or executing code in case of var or as in stand-in values for other variables.
These are of three types:
var – The var variable can be reassigned and is only accessible from within a function.
const – These variables can not be reassigned and are only accessible when they appear in the code.
let – The variable can be reassigned values but can only be declared once.
Arrays in any programming language are used as a way to sort out variables and properties into groups or categories. Hence, one specific group can have many variables which you can use for further programming.
Example: An array named ‘planet’ can have many variables like ‘earth,’ ‘Jupiter, ‘mars,’ ‘mercury,’ and more.
var planet = ["Earth", "Mars", "Jupiter", "Mercury"];
Arrays can be joined, combined, spliced, shifted, and more using array methods. Here are a few most used array methods in Javascript:
This basic cheat sheet for beginner programmers can help them get the hang of Javascript programming and get the most out of it.