Loading...

Interview Questions


What are the various data types that exist in JavaScript?


These are the different types of data that JavaScript supports:

  • Boolean - For true and false values
  • Null - For empty or unknown values
  • Undefined - For variables that are only declared and not defined or initialized
  • Number - For integer and floating-point numbers
  • String - For characters and alphanumeric values
  • Object - For collections or complex values
  • Symbols - For unique identifiers for objects

What are the features of JavaScript?


These are the features of JavaScript:

  • Lightweight, interpreted programming language
  • Cross-platform compatible
  • Open-source
  • Object-oriented
  • Integration with other backend and frontend technologies
  • Used especially for the development of network-based applications

How do you create an object in JavaScript?


Since JavaScript is essentially an  object-oriented scripting language, it supports and encourages the usage of objects while developing web applications.

const student = {

    name: 'John',

    age: 17

}

What are the scopes of a variable in JavaScript?


The scope of a variable implies where the variable has been declared or defined in a JavaScript program. There are two scopes of a variable:

Global Scope

Global variables, having global scope are available everywhere in a JavaScript code.

Local Scope

Local variables are accessible only within a function in which they are defined.

What are the conventions of naming a variable in JavaScript?


Following are the naming conventions for a variable in JavaScript:

  • Variable names cannot be similar to that of reserved keywords. For example, var, let, const, etc.
  • Variable names cannot begin with a numeric value. They must only begin with a letter or an underscore character.
  • Variable names are case-sensitive.

What are the ways of adding JavaScript code in an HTML file?


There are primarily two ways of embedding JavaScript code:

  • We can write JavaScript code within the script tag in the same HTML file; this is suitable when we need just a few lines of scripting within a web page.
  • We can import a JavaScript source file into an HTML document; this adds all scripting capabilities to a web page without cluttering the code.

How would you create a cookie?


The simplest way of creating a cookie using JavaScript is as below:

document.cookie = "key1 = value1; key2 = value2; expires = date";

How would you read a cookie?


Reading a cookie using JavaScript is also very simple. We can use the document.cookie string that contains the cookies that we just created using that string.

The document.cookie string keeps a list of name-value pairs separated by semicolons, where ‘name’ is the name of the cookie, and ‘value’ is its value. We can also use the split() method to break the cookie value into keys and values.

What are the arrow functions in JavaScript?


Arrow functions are a short and concise way of writing functions in JavaScript. The general syntax of an arrow function is as below:

const helloWorld = () => {

  console.log("hello world!");

};

What are the different ways an HTML element can be accessed in a JavaScript code?


Here are the ways an HTML element can be accessed in a JavaScript code:

  • getElementByClass(‘classname’): Gets all the HTML elements that have the specified classname.
  • getElementById(‘idname’): Gets an HTML element by its ID name.
  • getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified tagname.
  • querySelector(): Takes CSS style selector and returns the first selected HTML element.


Categories ( 117 )